很抱歉,我刚开始工作。我一直在尝试制作一个有几个选项 (def logged()
) 的菜单,并通过输入数字来跳转到具有特定目的的函数。然而,在 while 循环中放置 if 语句时,我似乎无法调用指定的函数,反而会跳回 menu()
函数,而 logged 函数应该永远通过 while 循环保持运行。
当我在 logged()
菜单中输入相应数字时,它应该调用那个特定的函数,但它却跳回到第一个菜单。我似乎无法使这两个菜单无限循环而不是互相跳转。那么,我应该如何使两个 while 循环分别循环并且不互相干扰呢?
def menu():
mode = input("""Choose options:\n
a) Test1 Calls logged() function
b) Test2
Enter the letter to select mode\n
> """)
return mode
def test1():
print("Test1")
logged()
def test2():
print("Test2")
def logged(): #Logged menu is supposed to run through a while loop and not break out when reached.
print("----------------------------------------------------------------------\n")
print("Welcome user. ")
modea = input("""Below are the options you can choose:\n
1) Function1
2) Function2
3) Function3
4) Exit
\n
Enter the corresponding number
> """).strip()
return modea
def funct1(): #EXAMPLE FUNCTIONS
print("Welcome to funct1")
def funct2():
print("Welcome to funct2")
def funct3():
print("Welcome to funct3")
#Main routine
validintro = True
while validintro:
name = input("Hello user, what is your name?: ")
if len(name) < 1:
print("Please enter a name: ")
elif len(name) > 30:
print("Please enter a name no more than 30 characters: ")
else:
validintro = False
print("Welcome to the test program {}.".format(name))
#The main routine
while True:
chosen_option = menu() #a custom variable is created that puts the menu function into the while loop
if chosen_option in ["a", "A"]:
test1()
if chosen_option in ["b", "B"]:
test2()
else:
print("""That was not a valid option, please try again:\n """)
while True:
option = logged()
if option == "1":
funct1()
elif option == "2":
funct2()
elif option == "3":
funct3()
elif option == "4":
break
else:
print("That was not a valid option, please try again: ")
print("Goodbye")