William
William

Reputation: 11

While loop and how to stop it in a function

I am having trouble with this data validation program I have for an assignment. I have trouble with this while loop here for some reason it continues to run indefinitely. Other while loops run in my code alright but this function here doesn't...

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

Had the code around the wrong way, both your answers were correct. @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()

Upvotes: 0

Views: 57

Answers (2)

Justin M. Ucar
Justin M. Ucar

Reputation: 883

The problem here is your indentation,
Do this:

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

the value assignment of pick parameter must be in while loop scope. Another thing is here is that main and login functions are not defined so make sure it is in your local scope or imported to the scope.

Upvotes: 1

Vagrant Storm
Vagrant Storm

Reputation: 13

I think you might have had some copy/paste errors while posting your code here...if not your print """ line is going to keep printing as long as pick is not equal to q or Q.

Upvotes: 0

Related Questions