我不太确定该怎么称呼这个职位。无论如何,我想做的就是将'diff'分配给用户输入,如果'diff'不是平均值或高级,则调用该函数,以便用户可以(希望)输入平均值或高级。但是,无论我输入了什么,即使输入是“平均”或“高级”,它也会始终调用该函数。代码 -def choices(): global diff diff = input("Choose a difficulty: Average/Advanced ") diff = diff.lower() x = 0 while x > 1: if diff == 'average': print('Difficulty set to average.') x = x + 1 elif diff == 'advanced': print('Difficulty set to advanced.') x = x + 1 if diff != 'average' or 'advanced': print('Your input is invalid. Please try again.') choices()choices()我做出的另一个与此类似的决定也发生了同样的事情,但我认为,如果遵循相同的逻辑,就没有必要放下它。抱歉,这是一个愚蠢的问题。我只是一个初学者。
2 回答
慕的地8271018
TA贡献1796条经验 获得超4个赞
您也可以将其包装到while循环中,这是python的新手,但是生成函数的递归实例对我来说似乎很危险。
def choices():
global diff
while true:
diff = input("Choose a difficulty: Average/Advanced ")
diff = diff.lower()
if diff == 'average':
print('Difficulty set to average.')
return
if diff == 'advanced':
print('Difficulty set to advanced.')
return
print('Your input is invalid. Please try again.')
慕桂英3389331
TA贡献2036条经验 获得超8个赞
您的第一个错误在于以下语句:
while x > 1:
您永远不会在该循环内执行代码,因为您在函数顶部设置了x = 0。当它到达while循环时,x = 0,因此while循环将被完全跳过。
还有许多其他问题,但这是阻止“ if”逻辑运行的原因。
我对此功能感到困惑,无法确切确定您要执行的操作,因此我无法提供完整有效的解决方案来解决您的问题,只能提供其中的第一个相当大的错误。
添加回答
举报
0/150
提交
取消