Try this, it avoids repeating yourself without needing a def
while True:
try:
number_of_iterations = int(raw_input("What is your favorite integer?"))
break
except ValueError:
print "Please put an integer!"
EDIT: Per the suggestions of the commenters, I have added break
to the try
portion of the block to eliminate the else
(the original remains as a reference below). Also, I changed "number" to "integer" because "3.14" would be invalid in the above code.
This was my original suggestion. The above is fewer lines (some may call this cleaner), but I prefer the below because to me the intent is clearer.
while True:
try:
number_of_iterations = int(raw_input("What is your favorite integer?"))
except ValueError:
print "Please put an integer!"
else:
break