-1

When user input is anything different from the specified letters "A", "a", "B", "b", "C", "c", "E", "e", it doesn't run the else statement but just closes. I would also like to have it repeat the program again if user input is not any of the specified letters, how can I achieve that? I believe it would be something I'd have to add in the else statement.

user_input = input()

if user_input in ("A", "a", "B", "b", "C", "c", "E", "e"):

    if user_input == "A" or user_input == "a":
        print("You chose A")

    elif user_input == "B" or user_input == "b":
        print("You chose B")

    elif user_input == "C" or user_input == "c":
        print("You chose C")

    elif user_input == "E" or user_input == "e":
        print("You chose to exit the program")

    else:
        print("Something went wrong, try again")
quamrana
  • 37,849
  • 12
  • 53
  • 71

2 Answers2

0

Your else should be part of the outer if (so same indentation), for now it is part of the inner if

Also you can simplify the tests, by first using .upper() to deal only with uppercase letters, and test the inclusion with user_input in "ABCDE"

user_input = input().upper()
if user_input in "ABCDE":
    if user_input == "A":
        print("You chose A")
    elif user_input == "B":
        print("You chose B")
    elif user_input == "C":
        print("You chose C")
    elif user_input == "E":
        print("You chose to exit the program")
else:
    print("Something went wrong, try again")

Ask again version

user_input = input("Please choose: ").upper()

while user_input not in "ABCDE":
    print("Something went wrong, try again: ")
    user_input = input().upper()

if user_input == "A":
    print("You chose A")
elif user_input == "B":
    print("You chose B")
elif user_input == "C":
    print("You chose C")
elif user_input == "E":
    print("You chose to exit the program")
azro
  • 53,056
  • 7
  • 34
  • 70
  • Thanks, that solved the issue! Any advice on how to repeat the code to first line if it gets to the else statement? – verycool Apr 06 '21 at 10:01
  • @verycool just add it. You can accept the answer if it satisfies you ;) – azro Apr 08 '21 at 16:44
0

Python is very indentation specific. This should do the trick:

user_input = input()
if user_input in ("A", "a", "B", "b", "C", "c", "E", "e"):

    if user_input == "A" or user_input == "a":
        print("You chose A")

    elif user_input == "B" or user_input == "b":
        print("You chose B")

    elif user_input == "C" or user_input == "c":
        print("You chose C")

    elif user_input == "E" or user_input == "e":
        print("You chose to exit the program")
else:
    print("Something went wrong, try again")
supercooler8
  • 503
  • 2
  • 7