我不知道为什么我在'except'上出现语法错误。在我看来一切都很好!这是我的代码:

def length():
gameLength = int(input("How many words do you want to play? You can chose anywhere from 1-40: "))
global gameLength
if gameLength <= 40 and gameLength >= 1:
    quit
else:
    int(input("Please choose a number between 1 & 40 "))
except ValueError = True:
     int(input("Please choose a number between 1 & 40 "))
return gameLength

最佳答案

您需要正确缩进代码,并在除外之前添加try语句。您还需要使用'=='而不是'='来评估true。

def length():
    global gameLength
    gameLength = int(input("How many words do you want to play? You can chose anywhere from 1-40: "))
    if gameLength <= 40 and gameLength >= 1:
        quit
    else:
        try:
            int(input("Please choose a number between 1 & 40 "))
        except ValueError == True:
            int(input("Please choose a number between 1 & 40 "))
    return gameLength

08-27 14:55