Why does the following not work? if letter == ('O' or 'Q'): statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does the following not work? if letter == ('O' or 'Q'): statement

It only does the ststement if 'O' is true, not if 'Q' is true.

28th May 2017, 2:03 PM
Andre Simmons
4 Answers
+ 13
if letter=="O" or letter=="Q": #......
28th May 2017, 2:05 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 7
In this way you are trying to compare the letter variable already to the boolean result of the or statement. So you tell Python to check if letter == True, not if it is equal to any of those chars. You should either decompose it like Valentin suggested or use the 'in' phrase: if letter in ['Q', 'O']: statement It is more flexible because you can easily add consecutive letters only and not another 'or letter==' phrase.
28th May 2017, 2:29 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
That worked, but i dont understand why the first only evaluates the first letter.
28th May 2017, 2:12 PM
Andre Simmons
0
Thank you both! That makes sense!
28th May 2017, 3:35 PM
Andre Simmons