Trouble with Python excersice - Convert letters to numbers in order (using ord and chr)

2.3k views Asked by At

I am sorry, but I am at a loss here everyone. No real idea where to start. I am new to Python and its been many years since I last tried programming of any kind. I was never very experienced. I am following this Python course online and have come to a dead end on an exercise. It is going off of the previous example.

x = int(input())
if x>=1 and x<=26:
    print('letter', x, 'in the alphabet:', chr(ord('A')+(x-1)))
else:
    print('invalid input:', x)

This example program converts numbers to letters. So, 'A' = 1 and 'B' = 2. It now wants me to make a program just like this but the opposite. I am supposed to start with this line:

letter = input()

Here is what the exercise describes:

Coding Exercise: 26 Letters 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

Now this is I can do, but not the way it wants me to do it, nor the way I should. I could do something like this:

if letter == 'A':
    letter = int(1)
    print(letter)
elif letter == 'B':
    letter = int(2)
    print(letter)

Of course that would be very long and sloppy. Does anyone have any advice for me? This one has me stuck. I'm sorry, I know this is probably very simple, this just seems much more complicated to me than it should be. I have tried looking this up but everything I find online is different enough that I cannot figure out how to implement it into this code. To be honest the chr and ord stuff confuses me greatly. I appreciate it for anyone who stuck around long enough to read this. Thank you. I have no idea what to do anymore with this one. Also, this is my very first time posting anything here, so if I did anything sloppy or wrong, I apologize and will delete/edit it as soon as I can.

The problem can be found here: https://cscircles.cemc.uwaterloo.ca/9-else-and-or-not/

Adding a little information. CScircles requires that A = 1 instead of 0. Thanks for the quick replies. I will be working on this again later today.

1

There are 1 answers

0
zimmerrol On

You have to use the ord function again, that you used in the previous exercise to get the char number of the character. Then you can substract the char value of the character A to get a value between 1 and 26.

inp = input()
if (len(inp) > 1 or inp != inp.upper()):
    print("invalid input")
else:
    print(ord(inp)-ord("A")+1)

The ord(x) function accepts a character x (e.g. any UTF8 character) and returns an integer. This integer value represents the number of the character in the encoding table: Let's take a look at the table for ascii below. Here you see the char values and the dec/int values of the characters. For example the character A is represented by the int value 65, and so on.

The chr(x) function is doing the opposite: it takes the int value and returns the char which is represented by this, this means:

ord("A") = 65
chr(65) = "A"
chr(ord(anyCharacter)) = anyCharacter