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:
第10行,语法错误:.format(x * 1))):,第1017行
我被告知这可以在2.6中使用,但它在3.2.3
中不起作用我正在尝试编写一个计算餐馆购买餐点总量的程序。该计划应要求用户输入食品费和销售税的百分比。然后该程序应询问用户他们想留下的小费百分比(例如:18%)。最后,该计划应显示食品的总费用,食品总费用的销售税(食品总费用*销售税率),餐食的小费(食品总费用*小费百分比),最后总额餐费(食品费+销售税+小费)。
答案 0 :(得分:2)
我想你可能想在那些输入语句中使用 strings :
x = float(input("What is/was the cost of the meal?"))
此外,可能在您的格式字符串(而不是{0}
)中使用{}
是一个好主意,至少如果您想与pre保持兼容-2.7 Python(虽然在这种情况下,我可能也在使用raw_input
)。即使在 2.7之后,我仍然更喜欢位置说明符,因为它使我更清楚。
此代码适用于我:
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)))
例如:
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
虽然您可能希望明确“百分比”应采用小数格式,但要避免输入20,因为您的提示将使服务员/女服务员非常开心。
或者,您可以将y
和z
除以100,将它们从百分比转换为分数。
答案 1 :(得分:1)
input(What is/was the cost of the meal?)
很糟糕。 input()
想要一个字符串作为参数。
input('What is/was the cost of the meal?')
这将在所有这三条线上发生。 python应该告诉你这些符号没有定义。
答案 2 :(得分:1)
您需要在字符串周围添加引号,例如x = float(input("What is/was the cost of the meal?"))
您还需要阅读the Python tutorial以了解Python的基础知识。