<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif;font-size:small;color:rgb(51,0,51)">Hi Danny,</div><div class="gmail_default" style="font-family:georgia,serif;font-size:small;color:rgb(51,0,51)"><br></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small;color:rgb(51,0,51)">Curious to the use the need of using while True in the given example of <span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">ask_for_a_digit().</span></div><div class="gmail_default" style="font-family:georgia,serif;font-size:small;color:rgb(51,0,51)"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 17, 2014 at 9:57 AM, Danny Yoo <span dir="ltr"><<a href="mailto:dyoo@hashcollision.org" target="_blank">dyoo@hashcollision.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> def ask_for_a_digit():<br>
> while True:<br>
> digit = raw_input("Give me a digit between 0 and 9.")<br>
> if digit not in "0123456789":<br>
> print "You didn't give me a digit. Try again."<br>
> else:<br>
> return int(digit)<br>
<br>
<br>
</span>Ooops. I made a mistake. ask_for_a_digit() is not technically quite<br>
right, because I forgot that when we're doing the expression:<br>
<br>
digit not in "0123456789"<br>
<br>
that this is technically checking that the left side isn't a substring<br>
of the right side. That's not what I wanted: I intended to check for<br>
element inclusion instead. So there are certain inputs where the<br>
buggy ask_for_a_digit() won't return an integer with a single digit.<br>
<br>
Here's one possible correction:<br>
<br>
###################################################<br>
<span class="">def ask_for_a_digit():<br>
while True:<br>
digit = raw_input("Give me a digit between 0 and 9.")<br>
</span> if len(digit) != 1 or digit not in "0123456789":<br>
<span class=""> print "You didn't give me a digit. Try again."<br>
else:<br>
return int(digit)<br>
</span>###################################################<br>
<br>
<br>
My apologies for not catching the bug sooner.<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div>