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

Posted

技术标签:

【中文标题】Python:在while循环中无法调用函数【英文标题】:Python: Unable to call function when in while loop 【发布时间】:2017-11-24 08:35:18 【问题描述】:

刚从这里开始。我一直在尝试通过输入一个数字来制作一个包含多个选项的菜单 (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")  

【问题讨论】:

【参考方案1】:

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

最大的问题是你进入了你的菜单循环(你拥有的第二个 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")  

【讨论】:

原则上,不要发布不包含代码的答案。当链接由于某种原因消失时,您的答案将变得毫无价值。您仍然可以链接到 repl.it,但也可以在此处复制您的代码。【参考方案2】:

问题是您的代码没有遵循您想要的流程,请尝试上面的代码,看看是否是您想要的,我会考虑一下并尝试解释我做了什么(现在我只是创建了一个函数 whileloop() 并将其添加到正确的位置)。

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

我知道发生了什么。我将列出您的代码正在经历的流程,您可能会以一种简单的方式理解它。

    进入循环while validintro; 进入while True循环(chosen_option = menu()) 输入menu() 并调用test1() 输入test1()并调用logged() 输入logged(),现在就是这样。当您在 While True 循环中调用 test1() 函数时,您的执行流程将返回。 你输入if chosen_option in ['b', 'B']。 因为它不在 b,B 内,您将激活您的 else 语句并打印您的错误消息。之后,循环重新开始。

【讨论】:

以上是关于Python:在while循环中无法调用函数的主要内容,如果未能解决你的问题,请参考以下文章

Python基础-----while循环练习

仅当在 while 循环中调用函数时,将文件发送到 blob 才有效

Python中while,for循环及文件操作,函数,模块等操作

Python递归函数相关

C语言调用一个函数无法跳出,死循环

在for循环中调用函数-python [关闭]