[英]“SyntaxError: Invalid Syntax” but it worked before! (Python 3.5.2 doing a tutorial for beginners)
如此我发誓这段代码以前工作过!
我正在尝试做一些非常简单的设置变量。
如您所见,这些工作:
>>> 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:无效语法”?
多谢你们! 对不起,我真是个菜鸟。
正如评论中多次指出的那样,您只是在程序的第4行上缺少了一个)
。 第4行应该看起来像
n = eval(input("Enter amount of coffee in pounds: "))# <--extra parenthesis
您的代码中一些不相关的点:
eval()
? 看起来您要进行float / integer转换,因此请使用int()
或float()
。 print("This calculates the cost of coffee.\\n")
n = float(input("Enter amount of coffee in pounds: "))*10.50
在将我的建议添加到您的代码中后,将产生如下内容:
def main():
print("This calculates the cost of coffee.\n")
n = float(input("Enter amount of coffee in pounds: "))*10.50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.