[Tutor] (no subject)

Andre Engels andreengels at gmail.com
Thu Nov 17 14:39:19 CET 2011


On Thu, Nov 17, 2011 at 2:14 PM, Nidian Job-Smith <nidianjs at hotmail.com>wrote:

>  Hi all,
>
> I'm new to programming (thus Python), so after reading the
> basics, I wanted to practise what I've learnt . I've come across a
> beginners exercise which is to programme rot13.
>
> I've written some code but it doesn't seem to work....
>
> Here it is:
>
>
> def rot13(s):
>     char_low = ()
>     result = ""
>     if not s.isalpha():
>         return char
>     char_low = char_low.lower()
>     if char_low <= 'm':
>                 dist = 13
>     else:
>                 dist = -13
>     char = chr(ord(char) + dist)
>
> def rot13_char(ch):
>   return ''.join( rot13(s) for char in ch )
>
>
> Any idea where i'm wrong?
>


Please, when posting a program that has problems to this list, do not just
say that "it doesn't work". Tell us what is wrong:
* If you get a syntax error, tell us that, and tell us where it is claimed
the syntax error is
* if you get an error message, give the error message as well as the stack
trace provided with it, or at least the last part of that stack trace
* If the program does not crash, but behaves different from what you
expected, tell us what you expected to get, and what you actually got (for
example, "After giving the input 5 and 3 I had expected the program to
output 8 and ask for a new pair of numbers, but instead it showed 8 lines
of 8 stars, printed 'Thank you for using my program' and ended.

Having said that, using your program gave me various problems. The first
was in return ''.join( rot13(s) for char in ch ) - I got an error message
that the name 's' was not defined. Reason: the variable before the for
should be the same as after, so you should either say 'rot13(s) for s in
ch' or 'rot13(char) for char in ch'

Most of your other problems had the same cause: If you want to use
something (a thing, a number, a letter, whatever), use the same name as you
used when you defined that thing.


-- 
André Engels, andreengels at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111117/d0d68354/attachment.html>


More information about the Tutor mailing list