-2

I wrote this code to ask the user for options A, B, or C and show a message afterward, but even if I type the correct options, it still sends me the 'try again' message. what can I do so the program runs correctly

option = str(input('\nChoose A, B or C: '))
while option.lower() != 'A' or 'B' or 'C':
    print('\nThat option is not availible, try again')
    option = str(input('\nChoose A, B or C'))
if option.lower() == 'A' or 'B' or 'C':
    print(f'You chose: {option}')

I was expecting the program to return the letter I choose only if it is A, B or C. otherwise print the 'try again message'

1
  • A lowercase letter will never be equal to either of A, B or C. Also, your or expression is flawed. input() returns a str object - i.e., no need to call str() Commented Jan 22, 2024 at 16:40

1 Answer 1

0

You could use a construct like this to ensure that the option is either A, B or C (case-insensitive):

while (option := input("\nChoose A, B or C: ")).lower() not in set("abc"):
    print('\nThat option is not available, try again')

print(f"You chose option {option}")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.