[Tutor] bubble sort function
Danny Yoo
dyoo at hashcollision.org
Mon Nov 17 05:27:47 CET 2014
> def ask_for_a_digit():
> while True:
> digit = raw_input("Give me a digit between 0 and 9.")
> if digit not in "0123456789":
> print "You didn't give me a digit. Try again."
> else:
> return int(digit)
Ooops. I made a mistake. ask_for_a_digit() is not technically quite
right, because I forgot that when we're doing the expression:
digit not in "0123456789"
that this is technically checking that the left side isn't a substring
of the right side. That's not what I wanted: I intended to check for
element inclusion instead. So there are certain inputs where the
buggy ask_for_a_digit() won't return an integer with a single digit.
Here's one possible correction:
###################################################
def ask_for_a_digit():
while True:
digit = raw_input("Give me a digit between 0 and 9.")
if len(digit) != 1 or digit not in "0123456789":
print "You didn't give me a digit. Try again."
else:
return int(digit)
###################################################
My apologies for not catching the bug sooner.
More information about the Tutor
mailing list