[Tutor] Converting letters to numbers?

Alan Gauld alan.gauld at btinternet.com
Wed Oct 9 00:45:46 CEST 2013


On 08/10/13 18:42, Kristen Jakupak wrote:
> I have to define a function add(c1, c2), where c1 and c2 are capital
> letters; the return value should be the sum (obtained by converting the
> letters to numbers, adding mod 26, then converting back to a capital
> letter).

That makes no sense to me.
Can you explain what you mean in more detail. I read it as:

def add(c1, c2):
     i1 = ord(c1)  # convert to numbers
     i2 = ord(c2)
     i1 += i1 % 26  # add mod 26 - why?
     i2 += i2 % 26
     return str(i1+i2).upper()  #add together and convert to upper case

That doesn't seem sensible to me.

> What I have so far is
> def add(c1, c2):
>      ans = ''
>      for i in c1 + c2:
>          ans += chr((((ord(i)-65))%26) + 65)
>      return ans

That seems different to what you described, and equally
non sensible?

Can you describe what the output should look like:

c1	c2	result
------------------------
a	b	???
c	d	???
A	a	???

Can you create a sample table showing input and
desired output? That might help.

And if you can't, how do you intend to test it?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list