[Tutor] Doctest error!

Dave Angel d at davea.name
Fri Nov 18 04:49:33 CET 2011


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



More information about the Tutor mailing list