4

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"
1
  • Using input in Python 2 is a bad idea concerning security. Use raw_input and cast the result to an int (or just work with strings). Commented Apr 3, 2013 at 5:54

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

The while-true-break pattern is common in Python. There's nothing particularly adverse about it, either - it's merely moving the intent of when loop termination occurs - either on condition check or during the running of the loop.
@Makoto I very much agree - at the same time, though, it's fairly standard to discourage overuse of break while in early stages of programming in order to help strengthen understanding of control flow and of different approaches (one couldn't simply break from a nested loop, for instance). That's just my take, though.
2

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

Comments

1
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.