为什么这不适用于python 3.2.3

时间:2012-09-24 04:30:50

标签: python python-3.x

  

可能重复:
  Python 3.2.3 programming…Almost had it working

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)))
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)))

输出错误:

第10行,在 语法错误:.format(x))):第1015行

有人报告说这在他们早期版本的python(我认为是v2.6)中有效。我正在使用后来的3.2.3并且为了这个版本没有工作原因而绞尽脑汁。这对我来说是有道理的,请给我启发。

3 个答案:

答案 0 :(得分:4)

第三次打印后您错过了一个结束括号:.format(x*(z/100)))

在修复括号之后,这是一个似乎是工作版本的内容:

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))
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)))

如果线宽小于79,也不需要换行。

答案 1 :(得分:2)

你错过了前一行的密切关注:

.format(x*(z/100))

答案 2 :(得分:2)

.format(x*(z/100))属于前一个print之后,有一个括号丢失。

应该是:

print ("Tip: ${}".format(x*(z/100)))

更新:不确定您是否正常工作,修复不平衡的括号后,这是完整的代码......

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))
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)))