简体   繁体   中英

python 2.7.3 while loop

I'm very new to python. I need to repeatedly loop, asking the user to select an option, then running the commands and repeating until the user chooses to exit. If the user selects any other option, the program must keep asking them to select a value until they choose a correct one. So far my program is not going that well. I'd like to keep to the while,if,elif conditions if possible. Is someone able to assist? Many thanks!

print """
How do you feel today?
1 = happy
2 = average
3 = sad
0 = exit program
"""

option = input("Please select one of the above options: ")
while option > 0 or option <=3:
    if option > 3:
        print "Please try again"
    elif option == 1:
        print "happy"
    elif option == 2:
        print "average"
    elif option == 3:
        print "sad"
    else:
        print "done"

The break command will exit a loop for you - however, in terms of beginning control flow, it's not really recommended either. Note, however, the user can never input a new value and therefore you will be caught in an infinite loop.

Perhaps try this:

running = True

while running:
    option = input("Please select one of the above options: ")
    if option > 3:
        print "Please try again"
    elif option == 1:
        print "happy"
    elif option == 2:
        print "average"
    elif option == 3:
        print "sad"
    else:
        print "done"
        running = False

This is how i would modify it to achieve the expected result.You where close but the if and else's shouldn't be inside the loop :).

print """
How do you feel today?
1 = happy
2 = average
3 = sad
0 = exit program
"""

option = input("Please select one of the above options: ")
while option >3:
    print "Please try again"
    option = input("Please select one of the above options: ")

if option == 1:
    print "happy"
elif option == 2:
    print "average"
elif option == 3:
    print "sad"
else:
    print "done"

Note you can use break to stop a loop at any time

Thanks Ben

import sys
option = int(input("Please select one of the above options: "))
while not option in (0, 1, 2, 3):
    option = int(input("Please select one of the above options: ")
    if option == 0: sys.exit()
    else: print "Please try again"
if option == 1:
        print "happy"
elif option == 2:
        print "average"
elif option == 3:
        print "sad"

The logic is that if the option is not 0, 1, 2, or 3, the program keeps asking for input. If it is within that range, the loop ends and it prints the result. If the input is 0, the program ends using sys.exit() .

Alternatively, you can use a dictionary to create a simpler, shorter program:

import sys
userOptions = {1: 'happy', 2: 'average', 3: 'sad'}
option = int(input("Please select one of the above options: "))
while not option in (0, 1, 2, 3):
    option = int(input("Please select one of the above options: ")
    if option == 0: sys.exit()
    else: print "Please try again"
print userOptions[option]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM