Python 3.x Python:在while循环中无法调用函数
刚从这里开始,索兹。我一直在尝试通过输入一个数字来创建一个包含多个选项的菜单(Python 3.x Python:在while循环中无法调用函数,python-3.x,function,while-loop,break,Python 3.x,Function,While Loop,Break,刚从这里开始,索兹。我一直在尝试通过输入一个数字来创建一个包含多个选项的菜单(def logged():),它会跳转到该函数并有目的。但是,我似乎无法使用放入while循环的if语句调用指定的函数,相反,当记录的函数应该永远在while循环中运行时,它会跳回menu()函数 当我在logged()的菜单中输入相应的数字时,它应该调用该特定函数,但它只是跳回第一个菜单。我似乎无法让这两个菜单永远循环,而不让它们来回跳跃。那么,我怎样才能使两个while循环永远分开循环,而不是彼此循环呢 def m
def logged()
:),它会跳转到该函数并有目的。但是,我似乎无法使用放入while循环的if语句调用指定的函数,相反,当记录的函数应该永远在while循环中运行时,它会跳回menu()
函数
当我在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")
def菜单():
模式=输入(“”选择选项:\n
a) Test1调用logged()函数
b) 测试2
输入字母以选择模式\n
> """)
返回模式
def test1():
打印(“测试1”)
日志()
def test2():
打印(“测试2”)
def logged():#logged菜单应该通过while循环运行,到达时不会中断。
打印(“----------------------------------------------------------------------------------------\n”)
打印(“欢迎用户”)
modea=input(“”)下面是您可以选择的选项:\n
1) 功能1
2) 功能2
3) 功能3
4) 出口
\n
输入相应的号码
>“”“).strip()
返回模式
def funct1():#示例函数
打印(“欢迎使用funct1”)
def funct2():
打印(“欢迎使用funct2”)
def funct3():
打印(“欢迎使用funct3”)
#主要程序
validintro=True
而validintro:
name=input(“你好,用户,你叫什么名字?:”)
如果len(名称)<1:
打印(“请输入名称:”)
elif len(姓名)>30:
打印(“请输入不超过30个字符的名称:”)
其他:
validintro=False
打印(“欢迎使用测试程序{}.”格式(名称))
#主要程序
尽管如此:
已选择的_option=menu()#将创建一个自定义变量,将菜单函数放入while循环
如果在[“a”,“a”]中选择了_选项:
test1()
如果在[“b”,“b”]中选择了_选项:
test2()
其他:
打印(“”这不是一个有效选项,请重试:\n“”)
尽管如此:
选项=logged()
如果选项==“1”:
funct1()
elif选项==“2”:
funct2()
elif选项==“3”:
funct3()
elif选项==“4”:
打破
其他:
打印(“该选项无效,请重试:”)
打印(“再见”)
好吧,你犯了一些错误(很明显),没什么大不了的,每个人都应该从某个地方开始学习
最大的问题是你进入了菜单循环(你的第二个while循环),但从来没有做任何事情来退出它。我还评论了一些其他的变化。我不是100%确定你在某些地方想要什么。。。但是
我想,我评论了这些变化。有一些奇怪的事情,我只是离开了,因为我觉得这就是我的意图
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 = False # I like it this way
while not 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 = True
print("Welcome to the test program {}.".format(name))
#The main routine
validintro = False # need a way out
while not validintro:
chosen_option = menu() #a custom variable is created that puts the menu function into the while loop
validintro = True # start thinking we're okay
if chosen_option in ["a", "A"]:
test1() # you're calling this, which calls the logged thing, but you do nothing with it
# I just left it because I figured that's what you wanted
elif chosen_option in ["b", "B"]: # You want an elif here
test2()
else:
print("""That was not a valid option, please try again:\n """)
validintro = False # proven otherwise
validintro = False
while not validintro:
validintro = True
option = logged()
print(option)
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: ")
validintro = False
print("Goodbye")
def菜单():
模式=输入(“”选择选项:\n
a) Test1调用logged()函数
b) 测试2
输入字母以选择模式\n
> """)
返回模式
def test1():
打印(“测试1”)
日志()
def test2():
打印(“测试2”)
def logged():#logged菜单应该通过while循环运行,到达时不会中断。
打印(“----------------------------------------------------------------------------------------\n”)
打印(“欢迎用户”)
modea=input(“”)下面是您可以选择的选项:\n
1) 功能1
2) 功能2
3) 功能3
4) 出口
\n
输入相应的号码
>“”“).strip()
返回模式
def funct1():#示例函数
打印(“欢迎使用funct1”)
def funct2():
打印(“欢迎使用funct2”)
def funct3():
打印(“欢迎使用funct3”)
#主要程序
validintro=False#我喜欢这样
虽然不是validintro:
name=input(“你好,用户,你叫什么名字?:”)
如果len(名称)<1:
打印(“请输入名称:”)
elif len(姓名)>30:
打印(“请输入不超过30个字符的名称:”)
其他:
validintro=True
打印(“欢迎使用测试程序{}.”格式(名称))
#主要程序
validintro=False#需要一条出路吗
虽然不是validintro:
已选择的_option=menu()#将创建一个自定义变量,将菜单函数放入while循环
validintro=True#开始认为我们没事
如果在[“a”,“a”]中选择了_选项:
test1()#您正在调用这个,它调用记录的东西,但您对它不做任何操作
#我把它留下是因为我想那是你想要的
elif在[“b”,“b”]中选择了_选项:#您想在此处使用elif吗
test2()
其他:
打印(“”这不是一个有效选项,请重试:\n“”)
validintro=False#证明为其他
validintro=False
虽然不是validintro:
validintro=True
选项=logged()
打印(可选)
如果选项==“1”:
funct1()
elif选项==“2”:
funct2()
elif选项==“3”:
funct3()
elif选项==“4”:
打破
其他:
打印(“该选项无效,请重试:”)
validintro=False
打印(“再见”)
问题是您的代码没有遵循您想要的流程,请尝试上面的代码,看看这是否是您想要的,我会考虑一下,并尝试解释一下我所做的事情(现在我刚刚创建了一个函数whileloop(),并将其添加到正确的位置)
def whileloop():
尽管如此:
选项=logged()
如果选项==“1”:
funct1()
elif选项==“2”:
funct2()
elif选项==“3”:
funct3()
elif选项==“4”:
打破
其他:
打印(“该选项无效,请重试:”)
打印(“再见”)
def菜单():
模式=输入(“”)选择选项
def whileloop():
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")
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")
whileloop()
def test2():
print("Test2")
whileloop()
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 """)