12

Ever wanted to have undefined behavior in Python? Do this in Python 2 (yes this was supposed to be a fibonacci number calculator with a limit but one of my classmates forgot the conversion just as seen below):
#Begin
def fib(n):
a,b=0,1
while a<n:
print a
a,b=b,a+b

fib(raw_input)
#End

Comments
  • 0
    Anyone so kind enough to tes this out? ;p currently unable to install python2
  • 3
    Well, it all boils down to a (int) being compared to the the built-in function raw_input (n). In Python 2, when a number is compared to to a non numeric object, the number is always less (for other incompatible types like list, tuple, str, etc., the comparison done lexicographically based on the type name). So, for example, '0' < 0 is False. The loop in the script above will not terminate. However, in Python 3 it should raise a TypeError for unorderable types.
  • 0
    @clickyotomy I know, but still...
Add Comment