刚从这里开始soz。我一直试图通过输入一个数字来创建一个带有几个选项的菜单(def logged()
:),并且它会跳转到该功能的目的。但是,我似乎无法使用while语句放入if语句来调用指定函数,而是在日志函数应该永久保持while循环运行时跳转到menu()
函数。Python:无法在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")
作为一个原则问题,不要发布不包含代码的答案。当链接由于某种原因消失时,你的回答将变得毫无价值。你仍然可以链接到repl.it,但也可以在这里复制你的代码。 – Tomalak
@Tomalak我gottcha好朋友。 –