Python Text Menu Infinite Loop

Python Text Menu Infinite Loop

Youre missing a line in menu, or else missing an input elsewhere. Youre not actually accepting a choice from your user yet. If you want to keep your current structure, menu should look like:

def menu():
    print options...
    return int(raw_input())

Or, a little cleaner (same effect):

def menu():
    return int(raw_input(options...))

Otherwise, you can just call menu() and then separately accept the users selection:

while loop == 1:
    menu()
    choice = int(raw_input())

Note that Ive changed your input() calls to int(raw_input()). This is a much safer way of accepting input from your user, since it prevents them from embedding arbitrary Python code in their input!


Not-quite-on-topic:

Now, just because I happened to notice it, Im also going to mention that loop is a potentially-misleading variable name. Since youre just using it as a boolean, you could rewrite the while loop like this:

loop = 1
while loop: # !
    #do stuff

This is kind of funny, but not very intuitive to read. Its usually better to simply loop indefinitely, and use break when your end condition is met:

while True:
    # Do stuff
    if choice == 5: #Or whatever end condition
        break

menu is a function that prints something and returns None. When you do choice=menu(), choice is set to None. None of your if match and the loop repeats endlessly.

A better option would be to prompt user for input in menu() and return the user input. Remember to convert user input to int first (because you compare choice to an integer)

choice = int(raw_input(Enter your choice:))
return choice

Python Text Menu Infinite Loop

Instead of printing options inside menu you should return them, because choice expects a return value from menu(), and as menu is not returning anything so the default return value None gets assigned to choice each time.

def menu():
        return blahblah options

loop = 1
choice = 0
while loop == 1:
    choice = menu()
    if choice == 1:     #this is always None
        add(input(Add this: ),input(to this: ))
    elif choice == 2:
        sub(input(Subtract this: ),input(from this: ))
    elif choice == 3:
        mul(input(Multiply this: ),input(by this: ))
    elif choice == 4:
        div(input(Divide this: ),input(by this: ))
    elif choice == 5:
        loop = 0

Instead of returning fixed values from menu you should take input from user:

def menu():
        strs = (Enter 1 for additionn
                Enter 2 for subtactionn
                Enter 3 for multiplicationn
                Enter 4 for divisionn
                Enter 5 to exit : )
        choice = raw_input(strs)
        return int(choice) 

while True:          #use while True
    choice = menu()
    if choice == 1:
        add(input(Add this: ),input(to this: ))
    elif choice == 2:
        sub(input(Subtract this: ),input(from this: ))
    elif choice == 3:
        mul(input(Multiply this: ),input(by this: ))
    elif choice == 4:
        div(input(Divide this: ),input(by this: ))
    elif choice == 5:
        break

Leave a Reply

Your email address will not be published.