gpt4 book ai didi

python - 为什么我在 'excpet'上收到语法错误?

转载 作者:行者123 更新时间:2023-12-02 11:14:38 25 4
gpt4 key购买 nike

我不知道为什么我在'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

关于python - 为什么我在 'excpet'上收到语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com