On 26/11/14 11:16, Sunil Tech wrote:
Thank you Alan. But a question here, how would it understand that the
given input is valid?
That's what the bit inside the loop does.
It checks whether the input string is a digit (specifically a decimal
digit).
If it is a digit it returns the result as an integer
otherwise it spits out an error and goes round the loop again.
PS Please don't top post. This thread is already getting hard to follow
after
only 2 exchanges!
On Mon, Nov 17, 2014 at 9:57 AM, Danny Yoo <d...@hashcollision.org
<mailto:d...@hashcollision.org>
<mailto:d...@hashcollision.org
<mailto:d...@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)
The while loop makes it keep on asking until a valid input is
received. Without the while loop it would only ask once and
either return None or a digit.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor