If statement giving me trouble

I am trying to figure out why the if statement keeps doing this. In this context, I am trying to make it so that if the prefix letter is J K L M N or P, the code will attach the prefix ‘ack’ and print the word. If the prefix letter is O or Q, I want it to put a u in the middle and then print it. For some reason my if stament is just not working and printing all of them with the u in the middle. It’s probably something super obvious but it’s very annoying for a beginner. Thanks.

Use if letter in ("O", "Q") instead.

if letter is “Q” then Python evaluates your code as:

if False or "Q"

which gives you “Q”, which is itself, “Q”, as a Python expression anywhere else, but as it is truthy, in the context of an if statement, the expression in your if statement is always True.

Checkout the very useful list of common gotchas in the Python docs (or PEPs?)

2 Likes

Legend, thank you.

Reference:

1 Like