while循环以及如何在函数中停止它

时间:2016-11-17 22:50:23

标签: python python-2.7 while-loop

我遇到了这项数据验证程序的问题。我在这里循环时遇到麻烦,因为某种原因它会继续无限期地运行。其他while循环在我的代码中运行正常,但此函数不在......

def menu():                                                                                             
    pick = None
    while pick != "q" or pick != "Q":
        print """
    \n

    Welcome to my password validation program.

    1 - New Account
    2 - Login
    q - Quit

    """
    pick = raw_input("What do you want to do?:  ")
    # Quit
    if pick == "q":
        print "\t   Thanks for coming Good-bye."
        raw_input("\nPress any key to exit")
    # New Account
    elif pick == "1":
        main()                                                                                      
    # Login
    elif pick == "2":
        if ps == 100:
            login()                                                                                 
        else:
            print "Sorry no password has been set. please create a new account."
    else:
        print "Sorry, but", pick, "isn't a valid choice."

menu()

如果代码错误的方式,你的答案都是正确的。 @jonrsharpe @tdelaney

   def menu():
        print \
        """
        \n

        Welcome to my password validation program.

        1 - New Account
        2 - Login
        q - Quit

        """

        pick = None
        pick = raw_input("What do you want to do?:  ")
        # Quit
        if pick == "q":
            print "\t   Thanks for coming Good-bye."
            raw_input("\nPress any key to exit")
        # New Account
        elif pick == "1":
            main()                                                                                      
        # Login
        elif pick == "2":
            if ps == 100:
                login()                                                                                 
            else:
                print "Sorry no password has been set. please create a new account."
        else:
            print "Sorry, but", pick,"isn't a valid choice."

    menu()

2 个答案:

答案 0 :(得分:1)

这里的问题是你的缩进,
这样做:

def menu():                                                                                             
    pick = None
    while pick != "q" or pick != "Q":
        print """
        \n

        Welcome to my password validation program.

        1 - New Account
        2 - Login
        q - Quit

        """
        pick = raw_input("What do you want to do?:  ")
        # Quit
        if pick == "q":
            print "\t   Thanks for coming Good-bye."
            raw_input("\nPress any key to exit")
        # New Account
        elif pick == "1":
            main()
        # Login
        elif pick == "2":
            if ps == 100:
                login()
            else:
                print "Sorry no password has been set. please create a new account."
        else:
            print "Sorry, but", pick, "isn't a valid choice."

menu()

pick参数的值赋值必须在while循环范围内。 另一件事是mainlogin函数未定义,因此请确保它在本地范围内或导入范围。

答案 1 :(得分:0)

我认为您在此处发布代码时可能会遇到一些复制/粘贴错误...如果不是您的print """行将继续打印,只要pick不等于q或Q.