I am currently writing a Tipper program for the job, and I am having a lot of issues with it. The result keeps saying invalid syntax, and I have switched up so many things but it seems like I can never make it just right. To give you an example of this, here is my current code:
print("Now that your belly is full, how much is the fee?")
fee = input("Enter your fee: ")
print("Would you like to give us a tip?")
answer = input("Yes or no?")
if answer == no:
why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
print("We understand. Here is your fee:", fee)
elif answer == yes:
tip = input("Would you like to tip 15 percent or 20 percent? ")
if tip == 15 :
cost1 = fee/15 fee
elif tip == 20 :
cost2 = fee/20 fee
else:
print("I do not understand.")
else:
print("I do not understand.")
print:("Have a wonderful day!")
So, yeah, to be honest with you I am not sure why it is not working, but could someone please answer? I am really stumped by this.
CodePudding user response:
I fixed the bugs in your code and tested it, however the overall code structure can still be improved.
So what caused your program to crash?
- "No" and "Yes" are both strings so you have to put them between " "
- You have to convert the fee entered from the user to integer using
int()
function so that you can do math calculations on it. - You were calculating the tip wrongly, I fixed it using these calculations
15% >> Total = 1.15xfee
20% >> Total = 1.20xfee
Note: in the below code I used themath.ceil()
function to round up the tip to a whole number.
To improve the user experience a bit, I also added a line that prints the total cost, including the tip, back to the user. Here is the corrected code:
import math
print("Now that your belly is full, how much is the fee?")
fee = int(input("Enter your fee: "))
print("Would you like to give us a tip?")
answer = input("yes or no?")
if answer == "no":
why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
print("We understand. Here is your fee:", fee)
elif answer == "yes":
tip = int(input("Would you like to tip 15 percent or 20 percent? "))
if tip == 15 :
cost = math.ceil(1.15 * fee)
print("Your total, including fee is, ", cost)
elif tip == 20 :
cost = math.ceil(1.20 * fee)
print("Your total, including fee is, ", cost)
else:
print("I do not understand.")
else:
print("I do not understand.")
print("Have a wonderful day!")
CodePudding user response:
Here is a version that works. Now, there are still problems. First, it doesn't catch if they enter garbage that is not a number for the bill or for the tip. Second, it doesn't round the tip, because I don't know what currency you're using. But at least you can run this and get an answer.
print("Now that your belly is full, how much is the bill?")
fee = float(input("Enter your bill: "))
print("Would you like to give us a tip?")
answer = input("yes or no?")
if answer.lower() == 'no':
why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
print("We understand.")
elif answer.lower() == "yes":
tip = int(input("What tip percentage do you use? "))
fee = fee * tip / 100
else:
print("I do not understand.")
print("Your total bill is", fee)
CodePudding user response:
As mentioned in the comments, you need to wrap the answers you're testing for in quotes (double quotes "
, or single quotes '
will do), because otherwise python will think they are variable names and not string literals, that you want to compare against.
Also, in the last print, you have a colon (:
) that doesn't belong there.
Here is your code with those two issues fixed.
print("Now that your belly is full, how much is the fee?")
fee = input("Enter your fee: ")
print("Would you like to give us a tip?")
answer = input("Yes or no?")
if answer == 'no':
why = input("Why do you not want to tip us? Also, you don't have to answer. When done, just press enter.")
print("We understand. Here is your fee:", fee)
elif answer == 'yes':
tip = input("Would you like to tip 15 percent or 20 percent? ")
if tip == 15:
cost1 = fee / 15 fee
elif tip == 20:
cost2 = fee / 20 fee
else:
print("I do not understand.")
else:
print("I do not understand.")
print("Have a wonderful day!")
Also, this way of hard coding options and no easy way to skip questions (once answered 'yes' for example) makes for a very frustrating user experience, but that is another issue.
Edit: You're also comparing strings to numbers, which will always be false, which saves you from the error, that trying to divide a string by a number would produce