【问题标题】:Getting a Syntax Error in python script在 python 脚本中出现语法错误
【发布时间】:2014-12-03 00:48:32
【问题描述】:

我不断收到似乎没有错误的语法错误。它是在带有 and 比较的 if 语句中引起的,我不确定为什么。这也是我使用python的第一个学期,所以请帮忙。

while userWins < 2 or CPUWins < 2:
    answer = random.randrange(3)
    print(str(answer))
    print("[1]-- Rock")
    print("[2]-- Paper")
    print("[3}-- Scissors")
    guess = int(input("Enter a number corresponding to a choice.")
    if guess == 1 and answer == 3:  #<--This is the line that keeps getting the syntax error
        userWins = userWins + 1
        print(name, " wins that round")
        RoundScore(userWins, CPUWins, name)
    elif guess == 1 and answer = 2:
        CPUWins += 1
        print(name, " loses that round")
        RoundScore(userWins, CPUWins, name)

这是 IDLE 给我的错误

Traceback(最近一次调用最后一次): 文件“E:/Cosc-151/Assignment5/a5_v34.py”,第 1 行,在 导入模组 文件“E:/Cosc-151/Assignment5\Mods.py”,第 61 行 如果猜测 == 1 并且答案 == 3: ^ SyntaxError: 无效语法

谢谢

【问题讨论】:

  • 很多时候你得到一个语法错误,实际的错误在前一行。在这种情况下,我猜你缺少一个 ')'

标签: if-statement syntax syntax-error comparison-operators


【解决方案1】:

您在上一行缺少一个 close-paren(关闭 int 调用)。

上线前你需要有

guess = int(input("Enter a number corresponding to a choice."))
                                                              ^
                                                       You forgot this

【讨论】:

    猜你喜欢
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    相关资源
    最近更新 更多