0

getting value from user

user_input = input("You decide: ")

#function that will print certain phrase based on user value also has a error message for wrong value(need loop for wrong value entered)

if user_input == 'A' or user_input == 'a':
    print("With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling")
elif user_input == 'Q' or user_input == 'q':
    mess = ("Well to heck with ya then!")
    print(mess)
elif user_input == 'B' or user_input == 'b':
    print("May Mother Russia led you to a brighter future!\nLets begin shall we")
elif user_input == 'C' or user_input == 'c':
    print("Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign")

else:
    print("Wrong Value!!!")

trying to loop the statement that if the user inputs a wrong value it will just start over. I am stuck and close to tossing my computer out, I know there is a simple fix but I can not seem to find it.

2 Answers2

1

One option may be to check if the user input is not in the valid inputs, then prompt in a while-loop until valid input is received:

user_input = raw_input("You decide: ").lower()

while (user_input not in ['a', 'b', 'c', 'q']):
    print("Wrong Value!!!")
    user_input = raw_input("You decide: ").lower()

if user_input == 'a':
    print("With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling")
elif user_input == 'q':
    mess = ("Well to heck with ya then!")
    print(mess)
elif user_input == 'b':
    print("May Mother Russia led you to a brighter future!\nLets begin shall we")
elif user_input == 'c':
    print("Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign")
else:
    print("Unhandled case")

and a cleaner way to print would be to use dict instead of so many ifs

user_input = raw_input("You decide: ").lower()

while (user_input not in ['a', 'b', 'c', 'q']):
    print("Wrong Value!!!")
    user_input = raw_input("You decide: ").lower()

message_dict = {
    'a':"With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling",
    'q':"Well to heck with ya then!",
    'b':"May Mother Russia led you to a brighter future!\nLets begin shall we",
    'c':"Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign",
    'default': "Unhandled case"
}

print(message_dict.get(user_input, 'default'))

This would be more pythonic too!

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
0

You could use the continue and break commands to create a straight forward loop:

while True:
    user_input = input("You decide: ")
    if user_input == ...
        print("With...
    elif user_input ==  ...
        print("Well...
    else:
        print("Wrong value!!!")
        continue
    break

That is not very Pythonic but would retro fit to your current code.

Here is another version with more structure to the program:

def query_and_response(prompt, responses):
    while True:
        try:
            return responses[input(prompt).lower()]
        except KeyError:
            print('Wrong value!!! valid values are {0}'.format(responses.keys()))

responses = {
    'a': 'With Uncle Sam in your corner you shall not fail...or I could be wrong?\nEither way lets get that Clock rolling',
    'b': 'May Mother Russia led you to a brighter future!\nLets begin shall we',
    'c': 'Ahhhh..the lonley Red State....well all I can say is either they follow you or get regret ever doubting you!!\nBegin your reign'
}
print query_and_response("You decide: ", responses)
Mike Robins
  • 1,733
  • 10
  • 14
  • `return` would always force the `while` to execute only once...right? – NoobEditor Dec 15 '17 at 04:55
  • @NoobEditor, yes the return from the function exits the loop. The `try except KeyError` branches if the input does not match one of the keys in the responses dictionary. – Mike Robins Dec 15 '17 at 05:47