Python 3 带有退出选项的菜单
Python 3 Menu with Quit option
我正在尝试在 Python 中创建一种 CLI 菜单(对此非常陌生)并且主要是退出选项有问题,它实际上不会退出并跳转到“Oooops那不对”选项,或者重复最后一步。如果你把它作为首选,它似乎确实有效
我知道我一定是在做一些愚蠢的事情。我试过将变量放在函数的末尾,以及菜单函数本身,但这似乎不起作用。
下面的片段,如果有人能指出我正确的方向。
def my_menu():
choice = input("Please choose an choice: ")
choice = choice.lower()
while (choice != "quit"):
if choice == "b":
a_thing()
my_menu()
if choice == "quit":
break
else:
print("Oooops that isn't right")
my_menu()
def a_thing():
print("a thing")
my_menu()
尝试在循环结束时再次输入选择,删除对 my_menu() 函数的调用并删除 if choice=="quit" 块(因为循环会自动退出when the choice is set to "quit")
def my_menu():
choice = input("Please choose an choice: ").lower()
while (choice != "quit"):
if choice == "b":
a_thing()
else:
print("Oooops that isn't right")
choice = input("Please choose an choice: ").lower()
def a_thing():
print("a thing")
my_menu()
或者您可以删除循环并仅使用 if 语句进行验证,在“退出”的情况下,您只需输入 return 即可打破循环
def my_menu():
choice = input("Please choose an choice: ").lower()
if choice == "b":
a_thing()
elif choice == "quit":
return
else:
print("Oooops that isn't right")
my_menu()
def a_thing():
print("a thing")
my_menu()
我 运行 您的代码,并且在其第一次迭代中按预期运行。之后,对my_menu()
的递归调用开始出现问题。
遍历它,首先你输入一些 运行dom 字符串,“hi”,它会进入 while 循环并使用 else
大小写。这将调用 my_menu()
,然后调用 另一个 while 循环。当你进入那个新的 while 循环时,你所做的任何退出(例如 break
)都不会退出第一个循环,只会退出你当前所在的循环,所以你处于无限循环中,因为你永远无法“回去”并在第一个循环中更改 choice
的值。
您可以通过对代码进行最少的更改来实现此行为的方法如下:
def my_menu():
choice = ""
while (choice != "quit"):
choice = input("Please choose an choice: ")
choice = choice.lower()
if choice == "b":
a_thing()
if choice == "quit":
break
else:
print("Oooops that isn't right")
def a_thing():
print("a thing")
my_menu()
(我删除了对 my_menu()
的递归调用,将输入行移至循环内,并在循环前初始化了 choice
)
我正在尝试在 Python 中创建一种 CLI 菜单(对此非常陌生)并且主要是退出选项有问题,它实际上不会退出并跳转到“Oooops那不对”选项,或者重复最后一步。如果你把它作为首选,它似乎确实有效
我知道我一定是在做一些愚蠢的事情。我试过将变量放在函数的末尾,以及菜单函数本身,但这似乎不起作用。
下面的片段,如果有人能指出我正确的方向。
def my_menu():
choice = input("Please choose an choice: ")
choice = choice.lower()
while (choice != "quit"):
if choice == "b":
a_thing()
my_menu()
if choice == "quit":
break
else:
print("Oooops that isn't right")
my_menu()
def a_thing():
print("a thing")
my_menu()
尝试在循环结束时再次输入选择,删除对 my_menu() 函数的调用并删除 if choice=="quit" 块(因为循环会自动退出when the choice is set to "quit")
def my_menu():
choice = input("Please choose an choice: ").lower()
while (choice != "quit"):
if choice == "b":
a_thing()
else:
print("Oooops that isn't right")
choice = input("Please choose an choice: ").lower()
def a_thing():
print("a thing")
my_menu()
或者您可以删除循环并仅使用 if 语句进行验证,在“退出”的情况下,您只需输入 return 即可打破循环
def my_menu():
choice = input("Please choose an choice: ").lower()
if choice == "b":
a_thing()
elif choice == "quit":
return
else:
print("Oooops that isn't right")
my_menu()
def a_thing():
print("a thing")
my_menu()
我 运行 您的代码,并且在其第一次迭代中按预期运行。之后,对my_menu()
的递归调用开始出现问题。
遍历它,首先你输入一些 运行dom 字符串,“hi”,它会进入 while 循环并使用 else
大小写。这将调用 my_menu()
,然后调用 另一个 while 循环。当你进入那个新的 while 循环时,你所做的任何退出(例如 break
)都不会退出第一个循环,只会退出你当前所在的循环,所以你处于无限循环中,因为你永远无法“回去”并在第一个循环中更改 choice
的值。
您可以通过对代码进行最少的更改来实现此行为的方法如下:
def my_menu():
choice = ""
while (choice != "quit"):
choice = input("Please choose an choice: ")
choice = choice.lower()
if choice == "b":
a_thing()
if choice == "quit":
break
else:
print("Oooops that isn't right")
def a_thing():
print("a thing")
my_menu()
(我删除了对 my_menu()
的递归调用,将输入行移至循环内,并在循环前初始化了 choice
)