0

I am making a simple text menu in Python and every time it gets to the while loop for the menu it just turns infinite.. I am not sure what's going on, I feel as if this should work correctly.

 # Menu system!
def menu():
        print "blahblah options"

loop = 1
choice = 0
while loop == 1:
    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:
        loop = 0

What is happening here that is causing it to loop through the print statement at the top endlessly?

4

3 回答 3

4

You're missing a line in menu, or else missing an input elsewhere. You're 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 user's selection:

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

Note that I've 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, I'm also going to mention that loop is a potentially-misleading variable name. Since you're 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. It's 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
于 2013-05-30T15:04:03.550 回答
1

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
于 2013-05-30T15:03:19.270 回答
1

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 addition\n'
                'Enter 2 for subtaction\n'
                'Enter 3 for multiplication\n'
                'Enter 4 for division\n'
                '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
于 2013-05-30T15:03:53.437 回答