问题描述
while循环一定有问题,但是我找不到它:(
def menu(): # first menu
print('+----------------------------+')
print('| first menu here |')
print('+----------------------------+')
print('|choose [1] |')
print('+----------------------------+')
print('|choose [2] to quit |')
print('+----------------------------+')
return input('>> ')
def s_menu(): # second menu
print('+**********************************+')
print('| second menu here |')
print('+**********************************+')
print('|choose [1] to continue |')
print('+**********************************+')
print('|choose [2] to back first menu |')
print('+**********************************+')
return input('>> ')
def doSomething():
print('Something here')
second_while() # declare second s_menu() here
def second_while(): # loop second s_menu()
while True:
resp = s_menu()
if resp == '1':
doSomething()
elif resp == '2':
menu()
break
else:
print('Wrong input!')
#break # if break was here,wrong user input will close terminal immediately
# loop first menu()
while True:
inp = menu()
if inp == '1':
doSomething()
elif inp == '2':
print('Break')
break
else:
print('Wrong input!')
#break # if break was here,wrong user input will close terminal immediately
解决方法
更改
def doSomething():
print('Something here')
second_while() # declare second s_menu() here
到
def doSomething():
print('Something here')
否则,每次调用doSomething()
时,您会更深入second_while()
一层,并且需要再按一次 2 来退出所有这些层。
好的,我找到了答案的解决方案。 while循环内的while循环
# loop first menu()
while True:
menu()
inp = input('>> ')
if inp == '1':
doSomething()
# loop doSomething()
while True:
resp = input('>> ')
if resp == '1':
doSomething()
elif resp == '2':
break
else:
print('Wrong input')
elif inp == '2':
print('Break')
break
else:
print('Wrong input!')