[Tutor] bubble sort function
Sunil Tech
sunil.techspk at gmail.com
Wed Nov 26 10:57:17 CET 2014
Hi Danny,
Curious to the use the need of using while True in the given example of
ask_for_a_digit().
On Mon, Nov 17, 2014 at 9:57 AM, Danny Yoo <dyoo at hashcollision.org> wrote:
> > 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.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20141126/16575d8f/attachment.html>
More information about the Tutor
mailing list