Weird behaviour?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Apr 21 21:05:11 EDT 2013


On Sun, 21 Apr 2013 17:37:18 -0700, jussij wrote:

> Can someone please explain the following behaviour?
> 
> I downloaded and compiled the Python 2.7.2 code base.
> 
> I then created this simple c:\temp\test.py macro:
> 
>     import sys
>     
>     def main():
>         print("Please Input 120: ")
>         input = raw_input()
>     
>         print("Value Inputed: " + input)
>     
>         if str(input) == "120":
>             print("Yes")
>         else:
>             print("No")
>     
>     main()
> 
> If I run the macro using the -u (flush buffers) option the if statement
> always fails:
>     
>     C:\Temp>python.exe -u c:\temp\test.py Please Input 120:
>     120
>     Value Inputed: 120
>     No


I cannot confirm that behaviour. It works fine for me.

Are you sure that the code you show above is *exactly* the code you're 
using? Actually, it cannot possibly be the exact code you are using, 
since it has been indented. What other changes did you make when retyping 
it in your message? Please copy and paste the actual code used, without 
retyping or making any modifications.

If the problem still persists, try printing repr(input) and see what it 
shows.

Also, a comment on your code: there is no need to call str(input), since 
input is already a string. If you replace the test:

str(input) == '120'

with just 

input == '120'

does the problem go away? If so, then there's something funny going on 
with the call to str(). Please print(str), and see what it shows.



-- 
Steven



More information about the Python-list mailing list