我不太确定该怎么称呼。
无论如何,我想做的就是将'diff'分配给用户输入,并且如果'diff'不是平均值或高级,请调用该函数,以便用户可以(希望)输入平均值或高级。 / p>
但是,无论我输入什么内容,即使输入是“平均”或“高级”,它也会始终调用该函数。
代码-
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()
我做出的另一个与此类似的决定也发生了同样的事情,但我认为,如果遵循相同的逻辑,就没有理由放弃它。
很抱歉,如果这是一个愚蠢的问题。我只是一个初学者。
答案 0 :(得分:0)
您也可以将其包装到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.')
答案 1 :(得分:0)
您的第一个bug在于以下语句:
while x > 1:
您永远不会在该循环内执行代码,因为您在函数顶部设置了x = 0。当它到达while循环时,x = 0,因此while循环将被完全跳过。
还有许多其他问题,但这是阻止“ if”逻辑运行的原因。
我对此功能非常困惑,无法确切确定您要执行的操作,因此无法提供完整有效的解决方案来解决您的问题,只能提供其中的第一个相当大的错误。