[Tutor] Doctest error!

Dave Angel d at davea.name
Fri Nov 18 05:24:16 CET 2011


On 11/17/2011 10:56 PM, Nidian Job-Smith wrote:
>
>
> ----------------------------------------
>> Date: Thu, 17 Nov 2011 22:49:33 -0500
>> From: d at davea.name
>> To: nidianjs at hotmail.com
>> CC: tutor at python.org
>> Subject: Re: [Tutor] Doctest error!
>>
>> On 11/18/2011 10:29 AM, John wrote:
>>> Hi all,
>>> When i run a doctest on this piece of code (shown at bottom) i get
>>> this error message [from the doctest]:
>>>
>>>
>>>
>>> Trying:
>>> rot13('5 The Parade')
>>> Expecting:
>>> '5 Gur Cnenqr'
>>> **********************************************************************
>>> File "F:\Uni\Rot13_1.py", line 12, in Rot13_1.rot13
>>> Failed example:
>>> rot13('5 The Parade')
>>> Expected:
>>> '5 Gur Cnenqr'
>>> Got:
>>> 'B-aur-]n\x7fnqr'
>>> Trying:
>>> rot13('5 Gur Cnenqr')
>>> Expecting:
>>> '5 The Parade'
>>> **********************************************************************
>>> File "F:\Uni\Rot13_1.py", line 14, in Rot13_1.rot13
>>> Failed example:
>>> rot13('5 Gur Cnenqr')
>>> Expected:
>>> '5 The Parade'
>>> Got:
>>> 'B-T\x82\x7f-P{r{~\x7f'
>>>
>>>
>>>
>>> An one have any idea why? (I'm guessing its to do with the numbers)
>>>
>>>
>>> code:
>>>
>>> def rot13(s):
>>>
>>> """
>>>>>> type(rot13("bob"))
>>> <type 'str'>
>>>>>> len(rot13("foobar"))
>>> 6
>>>>>> rot13("abc")
>>> 'nop'
>>>>>> rot13("XYZ")
>>> 'KLM'
>>>>>> rot13('5 The Parade')
>>> '5 Gur Cnenqr'
>>>>>> rot13('5 Gur Cnenqr')
>>> '5 The Parade'
>>> """
>>> result = '' # initialize output to empty
>>> for char in s: # iterate over string
>>> if int:
>>> char_low = s.lower()
>>> if char_low<= 'm':
>>> dist = 13
>>> else:
>>> dist = -13
>>> char = chr(ord(char) + dist)
>>> result+=char
>>> return result
>> The line "if int:" is clearly wrong. Did you write this code
>> yourself, or was it typed in from a listing somewhere? I'd assume that
>> you wanted to do some check on the char value. But if int will always
>> be true.
>>
>>
>>
>>
>> --
>>
>> DaveA
>
> I want it to look in s, and only perform this code on the letters in s(not numbers):
>   char_low = s.lower()            if char_low<= 'm':                    dist = 13            else:                dist = -13
>
Your formatting is messed up, and I can now see there at least one other 
bug in it anyway.

The method to check if a particular character is alphabetic is     
str.isalpha().  See if you can see what variable to call that on.  
Hint:  it's not s, since you want to check one character at a time in 
your loop.



-- 

DaveA



More information about the Tutor mailing list