I'm trying to write a program for my homework:
Write a program to determine how much to tip the server in a restaurant. The tip should be 15% of the check, with a minimum of $2.
The hint said to use an if statement, but i keep running into a alot of errors.
I don't know how else to write it, I've been trying to figure it out for a couple of hours now and haven't found proper answers online...
Check = 59
Tip = (Check)*(0.15)
if Tip > 2:
print(Tip)
CodePudding user response:
It's not clear whether only checks larger than $2 get tips, or whether tips must be at least $2.
I'm guessing the former, because you can do that with an if statement:
check = 59
if check > 2:
tip = 0.15*check
Or more compactly, like this:
tip = check*0.15 if check > 2 else 0
Whereas if the intent is that the tip must be at least $2, an if statement isn't really necessary:
check = 59
tip = max(2, 0.15*check)
But if you HAD to use an if statement (though max
is more elegant), you could do it like this:
check = 59
tip = 0.15*check
if tip <= 2:
tip = 2
But this is quite inelegant.
CodePudding user response:
First initialise your variable tip = 2
then if the tip percentage of 15% is higher apply the tip percentage to your variable tip
:
check = 30
tip = 2
tip_perc = check * 0.15
if tip_perc > 2:
tip = tip_perc
print(tip)
CodePudding user response:
It means you first use if
statement to see if the check is more than 2 before you do the math
check = 59
if check > 2:
tip = check * 0.15
print(tip)
Output:
8.85