【发布时间】:2016-10-06 02:41:51
【问题描述】:
我发誓这段代码以前有效!
是否有我可能不小心打开的 IDLE 设置?
我正在尝试做一些非常简单的事情..设置变量。
如您所见:
>>> print("hello")
hello
>>> def main():
print("hey")
>>>main()
hey
因此,当我尝试重新创建示例问题时,您会希望这些变量能够正常工作,对吗?
def main():
print("This calculates the cost of coffee.")
print()
n = eval(input("Enter amount of coffee in pounds: ")
m = 10.50 * n
SyntaxError: invalid syntax
为什么???为什么 Python 3.5.2 会返回“SyntaxError: invalid syntax”?
谢谢各位!对不起,我是个菜鸟。
【问题讨论】:
-
您在此行末尾缺少一个右括号:
n = eval(input("Enter amount of coffee in pounds: ")。所以你需要这个:n = eval(input("Enter amount of coffee in pounds: "))) -
专业提示 - 不要使用 eval。在不使用 eval 的情况下,总有更好的方法来做到这一点。您看起来想将其转换为 int/float。所以改为:
float(input("Enter amount of coffee in pounds: ")) -
您在第 4 行末尾缺少括号
n = ...)
标签: python