-1

可能重复:
Python 3.2.3 编程……几乎可以正常工作

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))):line 1015

有人报告说这在他们早期版本的python(我认为是v2.6)中有效。我正在使用后来的 3.2.3 并绞尽脑汁想知道为什么这不适用于这个版本。有人请启发我,这对我来说很有意义。

4

3 回答 3

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,也不需要换行。

于 2012-09-24T04:33:17.557 回答
2

您在前一行缺少一个紧密的括号:

.format(x*(z/100))
于 2012-09-24T04:33:30.620 回答
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)))
于 2012-09-24T04:34:11.307 回答