我在我的R.Pi上学习Python,并且遇到了一个小障碍。在我看来,下面的代码会将“inputchecker”功能保留在内存中,并返回到“getinput”功能。嵌套函数Pythonic?
这是错的代码?它应该完全不同吗?
def getinput(i):
if i == 1:
first = input("Would you like A or B? ")
inputchecker(1, first)
elif i == 2:
second = input("Would you like C or D? ")
inputchecker(2, second)
def inputchecker(n, userinput):
def _tryagain_(n):
usage(n)
getinput(n)
if n == 1:
if userinput in ("A", "B"):
print("You chose wisely.")
getinput(2)
else:
_tryagain_(n)
elif n == 2:
if userinput in ("C", "D"):
print("You chose wisely.")
else:
_tryagain_(n)
def usage(u):
if u == 1:
print("Usage: Just A or B please.")
if u == 2:
print("Usage: Just C or D please.")
getinput(1)
这不完全是Python的,因为这会导致if语句与不可预见的真正问题,这不是只蟒蛇限制。所有语言都支持这种“黑客”。最好使用while循环,除非您经常使用while循环将其放入函数中。 – ProgramFast