我想,随着主菜单启动一个程序,关闭功能不同的分支。在每个函数结束时,我希望用户要问'你想回到主菜单?如果用户说没有我想要的程序通过停止主循环终止(我不希望使用sys.exit()或任何类似)。示例代码:
#Issue is almost at the bottom
#Feel free to comment on the rest of the code as well,
#Always looking to improve
def main():
loop = True
while loop:
print('''MENU CHOICE''')
print('''1: go here''')
print('''2: go there''')
print('''3: You get the point''')
print('''0: Terminate program''')
print()
try:
answer = int(input('''I want to go to program: '''))
except:
print('''Not a valid menu choice, please try again''')
print()
if answer != 1 and answer != 2 and answer != 3 and answer != 0:
print('''Not a valid menu choice, please try again''')
print()
elif answer == 1:
program1()
elif answer == 2:
program2()
elif answer == 3:
program3()
else:
loop = False
def program1():
print('''This is program 1''')
itdontwork = input('''Do you want to go back to the menu? Y/N''')
if itdontwork == 'Y' or itdontwork == 'y':
print()
else:
print('''SHUTTING DOWN''')
loop = False #Here is the issue
#The rest of the programs would be the same
main()
最简单的方法就是让你的变量循环全球
loop = True
def main():
global loop
while loop:
print('''MENU CHOICE''')
print('''1: go here''')
print('''2: go there''')
print('''3: You get the point''')
print('''0: Terminate program''')
print()
try:
answer = int(input('''I want to go to program: '''))
except:
print('''Not a valid menu choice, please try again''')
print()
if answer != 1 and answer != 2 and answer != 3 and answer != 0:
print('''Not a valid menu choice, please try again''')
print()
elif answer == 1:
program1()
elif answer == 2:
program2()
elif answer == 3:
program3()
else:
loop = False
def program1():
global loop
print('''This is program 1''')
itdontwork = input('''Do you want to go back to the menu? Y/N''')
if itdontwork == 'Y' or itdontwork == 'y':
print()
else:
print('''SHUTTING DOWN''')
loop = False #Here is the issue
#The rest of the programs would be the same
main()
全球将允许你有相同的W / R变数无处不在。如果没有全球的所有变量本地。
问题是,你正在试图更改在program1
功能范围之外定义的变量。 loop
被内部main
定义因此仅main
可以访问它。有几种方法来解决这个问题,你可以声明loop
外(使其成为一个全球性),或只是让你的program1
返回一个boolean值以调用函数,例如:
def main():
loop = True
while loop:
loop = program1()
def program1():
itdontwork = input('''Do you want to go back to the menu? Y/N''')
if itdontwork == 'Y' or itdontwork == 'y':
print()
else:
print('''SHUTTING DOWN''')
return False
我想你想尝试做的是有不同的程序返回一个值。
问题是,该功能得到执行,但返回什么。你的主要功能没有返回值;因此,循环变量不能进行更新,以打破循环。
#Issue is almost at the bottom
#Feel free to comment on the rest of the code as well,
#Always looking to improve
def main():
loop = True
while loop:
print('''MENU CHOICE''')
print('''1: go here''')
print('''2: go there''')
print('''3: You get the point''')
print('''0: Terminate program''')
print()
try:
answer = int(input('''I want to go to program: '''))
except:
print('''Not a valid menu choice, please try again''')
print()
if answer != 1 and answer != 2 and answer != 3 and answer != 0:
print('''Not a valid menu choice, please try again''')
print()
elif answer == 1:
return program1() # Return the output of this function
elif answer == 2:
return program2() # Return the output of this function
elif answer == 3:
return program3() # Return the output of this function
else:
loop = False
def program1():
print('''This is program 1''')
itdontwork = input('''Do you want to go back to the menu? Y/N''')
if itdontwork == 'Y' or itdontwork == 'y':
print()
else:
print('''SHUTTING DOWN''')
return False # Return the output of this function
#The rest of the programs would be the same
main()
...
elif answer == 1:
loop = program1()
elif answer == 2:
loop = program2()
elif answer == 3:
loop = program3()
else:
loop = False
def program1():
print('''This is program 1''')
itdontwork = input('''Do you want to go back to the menu? Y/N''')
if itdontwork == 'Y' or itdontwork == 'y':
print()
return 1 # back to menu...
else:
print('''SHUTTING DOWN''')
return 0
这将让你想要的,你从被调用函数需要在循环中的数据。
你可以在它raise ValueError('Words and stuff')
然后陷阱。
if answer != 1 and answer != 2 and answer != 3 and answer != 0:
print('''Not a valid menu choice, please try again''')
print()
elif answer == 1:
try:
program1()
except ValueError:
break
elif answer == 2:
try:
program2()
except ValueError:
break
elif answer == 3:
try:
program3()
except ValueError:
break
else:
loop = False
def program1():
print('''This is program 1''')
itdontwork = input('''Do you want to go back to the menu? Y/N''')
if itdontwork == 'Y' or itdontwork == 'y':
print()
else:
print('''SHUTTING DOWN''')
# loop = False #Here is the issue
raise ValueError('BOOM SHAKA LAKA!')