The or
does not work as you imagine, the correct would be:
prefixes = "JKLMNOPQ"
for letter in prefixes:
if letter[0] == "Q" or letter[0] == "O":
print(letter + "uack")
else:
print(letter + "ack")
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
You were comparing the expression letter[0] == "Q"
which may be true or false according to the value of letter[0]
. How do you have a or
the only way the decision can be made without evaluating anything else is when this expression is true, because it is certain that the block must be executed. And so far so good. The or
is also appropriate there. The problem is in the second expression of or
that does not return what you expect.
What is the boolean result of "O"
? By definition, all values that are not considered zeroed or empty are false, all other values are true, so "O"
is true. So every time the first expression gives false and he will try the second this is always true, and therefore makes everything be true.
What you really wanted to do is letter[0] == "O"
because then the expression can be true at the right time, but will be false in most cases.
The other changes I made because it’s more readable and obvious
Thank you for clearing my doubt.
– kdz