1

I am relatively new to python and I cannot get this simple string comparison to work. No matter what I type into the terminal, even if I type 'a', I get "Invalid Input". If anybody can help me find what I am doing wrong that would be great. I have also tried to assign answer choices in variables (ans1 = 'a', ans1 = 'b') but it seems to behave the same. Thank you

while True:
    pref = (input('Choose a or b '))

    if (pref != 'a') or (pref !='b'):
        print('Invalid input')
        time.sleep(2)
        continue
    else:
        break

print('same')
gmeeker99
  • 33
  • 3
  • 3
    `pref != 'a' or 'b'` is _not_ the same as `(pref != 'a') or (pref != 'b')` – ForceBru Feb 23 '21 at 13:48
  • Put this a or b inside the round brackets. ('Choose a or b') while True: pref = input('Choose a or b') if pref != ('a' or 'b'): print('Invalid input') time.sleep(2) continue else: break print('same') – Aditi Sharma Feb 23 '21 at 13:57
  • I have updated the code to include the parenthesis. I still get invalid input when typing 'a' or 'b'. – gmeeker99 Feb 23 '21 at 14:05
  • Try this: `if not ((pref == 'a') or (pref == 'b'))`: – segj Feb 23 '21 at 14:37
  • `if (pref != 'a') and (pref != 'b'):` also works and is shorter and perhaps more readable. – segj Feb 23 '21 at 14:49

0 Answers0