It is worth noting that even if the priority was different, this would not work:
if letter == ("O" or "Q"):
This is not good, since "O" is true-ish *, so "O" or "Q" is āOā, so we just compare the letter with "O" and completely ignore "Q" .
In English, we consider "O" or "Q" abstract concept that is non-deterministic not "O" or "Q" according to what logic requires. Computers, however, deal with deterministic quantities. Closest to all, can we move on to the English phrasing Is the letter "O" or "Q"? (and more precisely, while we are on it :)) is Is the letter one of the following: "O", "Q"? that we can formalize a bit as Is the letter in the following set: {"O", "Q"}?
Python allows us to easily create sets (with the set keyword), but we still need to be explicit in that we check if there is a letter in this set (i.e. we check for set membership , not equality). Fortunately, we strictly do not need a membership verification kit. (If we had a huge number of elements for testing, and we only needed to build the set once, but I had many letters for testing, then we could save time in general by formally creating set , as they are optimized for test membership; with tuple like ("O", "Q") , Python should check each element one at a time, but here tuple works just fine.)
In short: you fix the problem as others have said, but their explanation of the problem should leave you asking more questions (which I hope I answered here).
source share