You don't need any loops to solve this. Firstly, realize that you repeat the first test in each elif
, so let's pull that right out to the top:
if x < ord('A')
Now, there are only a limited range of possible values for our x
. In fact, the valid values for x
also include x < ord('A')
so we can remove that test:
if x < 65 or x > 72:
print("invalid")
Ok, now we only have valid results left. There's actually a very simple formula for what you print: it ends up being x - 64
. So that can go in the else
part:
else:
print(x - 64)
So the whole thing ends up being:
x = ord(letter)
if x < 65 or x > 72:
print('invalid')
else:
print(x - 64)
Note that the capital letters span more than this. However, the fix should be fairly simple.