Python - Caeser Cipher Not Giving Right Output

Dave Angel davea at davea.name
Fri Mar 21 05:02:39 CET 2014


 dtran.ru at gmail.com Wrote in message:
> On Thursday, March 20, 2014 11:16:50 PM UTC-4, Dave Angel wrote:
>> dtran.ru at gmail.com Wrote in message:
>> > 
>> 
>> > def two(c1 , c2):
>> 
>> >     c1 = chartonum(c1)
>> 
>> >     c2 = chartonum(c2)
>> 
>> >     return numtochar(c1 + c2 %26)
>> 
>> 
>> 
>> You're missing some parentheses in that line. To test your
>> 
>>  understanding,  try picking some numbers for c1 and c2. Display
>> 
>>  c1 + c2 % 26, and see if the result is always between 0 and
>> 
>>  25.
>> 
>> 
>> 
>> Or look up the term precedence in your textbook.
>> 
>
>> DaveA
> 
> Thanks for your input Dave. Would the line be:
> 
> return numtochar(c1 + c2 %26) 

That would be the line I tagged,  yes.

> 
> c1 and c2 are lower-case letters. 

No, they're not , you just got done overwriting the letters with
 numbers.   That's a bad habit, reusing local variables with data
 of different meanings,  but I was trying to focus on the line
 with the bug.


> And I was wondering how I would add the partenthesis because I tried:
> 
> return numtochar(c1 + c2 (%26)) and it gave me an error. 

Please help us to help you by actually showing the traceback. 
 Doesn't matter in this case, but...

What order do you want the add and the modulo to happen?  Use the
 parentheses to say so.  Or split it into two or more lines, with
 another variable. 

Perhaps you don't understand that % is an operator,  just like +
 and *.  If you were told to add a and b before multiplying by c,
 how would you write that? Try it, just as you tried the other
 experiment I recommended. 




-- 
DaveA




More information about the Python-list mailing list