[Tutor] Rot13
Dave Angel
d at davea.name
Thu Nov 17 19:34:05 CET 2011
On 11/17/2011 12:54 PM, Nidian Job-Smith wrote:
> Sorry about the code format in last E-mail. I'll attach the code in notepad, as my e-mail doesnt seem to like sending plain text..
>
But attachments aren't visible to everyone on the list, and even when
they are, some people are (rightfully) paranoid about arbitrarily
opening attachments.
You don't name your email program, but if it cannot handle text
messages, perhaps you should look for another. There are many free
email programs. For example, I use Thunderbird. I know it works under
Windows, since I used to use it that way. But I'm currently using it
under Linux.
> ----------------------------------------
>> From: nidianjs at hotmail.com
>> To: steve at pearwood.info; tutor at python.org
>> Date: Thu, 17 Nov 2011 17:45:11 +0000
>> Subject: [Tutor] Rot13
>>
>>
>> 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 write the code for rot13. I've written some code but it doesn't seem to work....
>> When I run it I get this error:
>>
>>
>> NameError: global name 'rot13_char' is not defined
>>
That's not the whole error message/
How can you get this error when the file you attached never used that
name ? Be sure and use cut 'n paste when telling about an error
message. If you don't know how, ask, but be sure and give your
environment specifics.
Further, the file you attached wouldn't even try to run anything, so it
couldn't get any such error, even if we concede a typo in the name.
When I run it (after renaming it from a .txt extension to a .py one), I
get the following:
davea at think:~/temppython$ mv rot13_code-1.txt rot13_code-1.py
davea at think:~/temppython$ python rot13_code-1.py
davea at think:~/temppython$
Now, if I add a couple of lines to it to test it out:
print rot13("abc")
print rot13("XyzzY")
Then I indeed get an error:
davea at think:~/temppython$ python rot13_code-1.py
Traceback (most recent call last):
File "rot13_code-1.py", line 32, in <module>
print rot13("abc")
File "rot13_code-1.py", line 30, in rot13
return ''.join( rot13_low(char)for char in string )
File "rot13_code-1.py", line 30, in <genexpr>
return ''.join( rot13_low(char)for char in string )
NameError: global name 'rot13_low' is not defined
Guess what, you had two functions with the same name. So the first one
disappears. Apparently you wanted to name it rot13_low():
def rot13_low(s):
Now, when I run it, I get:
davea at think:~/temppython$ python rot13_code-1.py
Traceback (most recent call last):
File "rot13_code-1.py", line 32, in <module>
print rot13("abc")
File "rot13_code-1.py", line 30, in rot13
return ''.join( rot13_low(char)for char in string )
File "rot13_code-1.py", line 30, in <genexpr>
return ''.join( rot13_low(char)for char in string )
File "rot13_code-1.py", line 22, in rot13_low
char_low = char_low.lower()
AttributeError: 'tuple' object has no attribute 'lower'
No idea why you created a tuple called char_low. My guess is that you
meant the formal parameter of the first function to be char_low, since
calling it s would be misleading. It's intended to be a single
character, not a string.
Anyway, you can presumably see the process. Look at the whole error
traceback, and if you can't see what it means, ask for help.
BTW, the docstring you have for the first function belongs in the second
one. That first function doesn't deal with strings.
--
DaveA
More information about the Tutor
mailing list