Python Efficiency - Better version than multiple if elif statements - use a for loop - chr ord conversions?

2k views Asked by At

I'm taking an intro to an online Python course and here's the problem description.

Write a program which does the reverse of the example above: it should take a character as input and output the corresponding number (between 1 and 26). Your program should only accept capital letters. As error-checking, print invalid if the input is not a capital letter.

Can someone reply with a more efficient way of writing the following?

letter = input()
x = ord(letter)
if x >= ord('A') and x == 65:
   print(1)
elif x >= ord('A') and x == 66:
   print(2)
elif x >= ord('A') and x == 67:
   print(3)
elif x >= ord('A') and x == 68:
   print(4)
elif x >= ord('A') and x == 69:
   print(5)
elif x >= ord('A') and x == 70:
   print(6)
elif x >= ord('A') and x == 71:
   print(7)
elif x >= ord('A') and x == 72:
   print(8)


else:
   print("invalid")

I was thinking of some kind of for loop. I started several versions of for loops, but can't figure out how to handle the iterator and correspond it to the increasing asci value. Thanks! This chr ord chart could help

4

There are 4 answers

2
Yuushi On BEST ANSWER

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.

0
sl8r000 On

Without giving away the full solution: You could [1] Check first if x is not a capital letter (check out string.uppercase), and then [2] (Now that you know x is indeed a capital letter), return the ord(x) - n where n is the order at which the capital letters start (try ord('A') - 1).

Hope that helps!

0
Arpit Agrawal On
ord(letter)-64 if 65<=ord(letter)<=90 else 'Invalid'
0
Ahmad Dwaik 'Warlock' On
letter = input()
x = ord(letter)
if x>64:
    print x-64
else:
    print "invalid"

more compact wit turnary operator

x = ord(input())
print (x-64 if x>64 else 'invalid')