[Tutor] Converting letters to numbers?

Dave Angel davea at davea.name
Wed Oct 9 02:02:50 CEST 2013


On 8/10/2013 13: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).
>
> 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
>
> Any guidance would be great, thanks.
>
>
> <div dir="ltr"><span style="font-family:Arial,Helvetica,sans-serif;font-size:13px">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). </span><div>
>
> <font face="Arial, Helvetica, sans-serif"><br></font></div><div>What I have so far is</div><div><span style="font-family:Arial,Helvetica,sans-serif;font-size:13px">def add(c1, c2): </span><br style="font-family:Arial,Helvetica,sans-serif;font-size:13px">
>
> <span style="font-family:Arial,Helvetica,sans-serif;font-size:13px">    ans = '' </span><br style="font-family:Arial,Helvetica,sans-serif;font-size:13px"><span style="font-family:Arial,Helvetica,sans-serif;font-size:13px">    for i in c1 + c2: </span><br style="font-family:Arial,Helvetica,sans-serif;font-size:13px">
>
> <span style="font-family:Arial,Helvetica,sans-serif;font-size:13px">        ans += chr((((ord(i)-65))%26) + 65) </span><br style="font-family:Arial,Helvetica,sans-serif;font-size:13px"><span style="font-family:Arial,Helvetica,sans-serif;font-size:13px">    return ans </span><br>
>
> </div><div><span style="font-family:Arial,Helvetica,sans-serif;font-size:13px"><br></span></div><div><font face="Arial, Helvetica, sans-serif">Any guidance would be great, thanks.</font></div></div>
>
>

I suggest you read the other thread you started.  And getting more
specific, don't try to do everything on one line.  Factor out the logic
so you can test each part independently.

And add comments describing what each line is trying to do.  That way,
you'll have a chance of figuring out why things aren't working.

After it all works, if you're trying to impress someone, you can squeeze
it down to one cryptic line again.


-- 
DaveA





More information about the Tutor mailing list