Issue
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)
Solution
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.
Answered By - Vin Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.