python while函数_Python:无法在while循环中调用函数

好的,所以你犯了一些错误(很明显),没什么大不了的,每个人都得开始学习。

最大的问题是你进入菜单循环(你有第二个while循环),但从不做任何事情来退出它。我还评论了其他一些变化。我不是100%确定你在某些地方做什么...但是...

我认为this is what you were going for though,我评论了这些变化。有一些奇怪的东西,我只是留下,因为我认为这是意图。

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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值