可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
x = float(input("What is/was the cost of the meal?")) y = float(input("What is/was the sales tax?")) z = float(input("What percentage tip would you like to leave?")) print ("Original Food Charge: ${}" .format(x*1))) print ("Sales Tax: ${}" .format((y/100)*x))) print ("Tip: ${}" .format(x*(z/100))) print ("Total Charge For Food: ${}" .format(x+((y/100)*x)+((z/100)*x))) error output:
line 10, in Syntax Error: .format(x*1))):, line 1017
I have been told that this works in 2.6 but it does not work STILL in 3.2.3
I'm attempting to write a program that calculates the total amount of a meal purchased at a restaurant. The program should ask the user to enter the charge for the food and the percentage of the sales tax. The program should then ask the user what percentage of tip they would like to leave (Example: 18%). Finally the program should display the total charge for the food, the sales tax on the total charge for the food (total food charge * sales tax rate), the tip for the meal (total food charge * tip percentage), and finally the total cost of the meal (food charge + sales tax + tip).
回答1:
I think you may want to use strings in those input statements:
x = float(input("What is/was the cost of the meal?"))
In addition, it may be a good idea to use {0}
in your format strings (rather than {}
), at least if you want to keep compatible with pre-2.7 Python (although in that case, I'd probably be using raw_input
as well). Even after 2.7, I still prefer the positional specifiers since it makes it clearer for me.
This code works fine for me:
x = float(input("What is/was the cost of the meal?")) y = float(input("What is/was the sales tax?")) z = float(input("What percentage tip would you like to leave?")) print ("Original Food Charge: ${0}".format(x)) print ("Sales Tax: ${0}".format(y*x)) print ("Tip: ${0}".format(x*z)) print ("Total Charge For Food: ${0}".format(x+(y*x)+(z*x)))
such as with:
What is/was the cost of the meal?50 What is/was the sales tax?.05 What percentage tip would you like to leave?.1 Original Food Charge: $50.0 Sales Tax: $2.5 Tip: $5.0 Total Charge For Food: $57.5
Though you may want to make it clear that the "percentages" should be in fractional format, lest entering 20 as your tip will make the waiter/waitress very happy.
Or, you can divide y
and z
by 100 to turn them from percentages to fractions.
回答2:
input(What is/was the cost of the meal?)
Is bad. input()
wants a string as an argument.
input('What is/was the cost of the meal?')
This is going to happen on all three of those lines. python should tell you that these symbols are not defined.
回答3:
You need to put quotes around your strings, e.g., x = float(input("What is/was the cost of the meal?"))
You also need to read the Python tutorial to learn the basics of Python.