On 01/05/14 01:18, jordan smallwood wrote:
Hey there,
I have this code below (in to cm conversion) and I want to have the user
try again if they enter in a non integer. What am I missing:
A loop.
There is a common pattern or idiom in Pytthon:
while True:
get input
if input ok:
break # exits the loop
else:
print error # and go round the loop again
So in your case it will look like this:
while True:
try:
value = float(raw_input('Please enter a number: '))
break # only gets here if no error
except ValueError:
print "Not a valid number."
Those two extra lines are all you need.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor