繁体   English   中英

“ SyntaxError:无效的语法”,但以前可以使用! (Python 3.5.2为初学者做一个教程)

[英]“SyntaxError: Invalid Syntax” but it worked before! (Python 3.5.2 doing a tutorial for beginners)

如此我发誓这段代码以前工作过!

我可能不小心打开了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:无效语法”?

多谢你们! 对不起,我真是个菜鸟。

正如评论中多次指出的那样,您只是在程序的第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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM