【问题标题】:Python unexpected EOF while parsing 2.7.10 [duplicate]解析2.7.10时Python意外EOF [重复]
【发布时间】:2015-09-22 15:55:30
【问题描述】:

这是一些未完成的创建计算器的代码,但是它一直给我一个语法错误。

n1=input("Enter Number: ")
Eo=input("Enter Operator: ")
if Eo=="+" or "-" or "*" or "/":
    answer= n1+str(Eo)+input("Enter 2nd number: ")

Traceback (most recent call last):
  File "C:/Python27/Programming/Calculator.py", line 2, in <module>
    Eo=input("Enter Operator: ")
  File "<string>", line 1
    +
    ^
SyntaxError: unexpected EOF while parsing

【问题讨论】:

  • 这条线并没有像你想象的那样做:if Eo=="+" or "-" or "*" or "/":
  • 另请参阅 How do I test one variable against multiple values?,因为您在 if 测试中出现逻辑错误。
  • 那么它会做什么?,我希望它在选择其中一个运算符时进行答案计算..
  • 请写一个写一百次的程序:“在我能详细解释它的作用之前,我不会在 Python 2.x 中使用input。”

标签: python


【解决方案1】:

两个问题:

1) 使用raw_input 而不是input

2) 要针对多个值检查 Eo,请使用

if Eo in ("+", "-", "*", "/"):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    相关资源
    最近更新 更多