[Tutor] loop questions
Mark Lawrence
breamoreboy at yahoo.co.uk
Sun Apr 7 00:40:11 CEST 2013
On 06/04/2013 23:00, Soliman, Yasmin wrote:
> I have two questions on these simple programs:
>
> 1st why does this loop keep repeating after I enter 'Quit'?
>
> import calendar
> m = raw_input(“Enter a year: “)
> while m != “Quit”:
> if calendar.isleap(int(m)):
> print “%d is a leap year” % (int(m))
> else:
> print “%d is not a leap year” % (int(m))
>
Already answered by Mitya Sirenef.
>
> 2nd How can I make this program not crash when a user enters a non integer?
The program doesn't crash, it raises an exception.
>
> m = raw_input(“Enter an integer: “)
> while not m.isdigit():
> m = raw_input(“Enter an integer: “)
> num = int(m)
try:
num = int(m)
except ValueError:
doSomething
I'll leave you to restructure the loop.
--
If you're using GoogleCrap™ please read this
http://wiki.python.org/moin/GoogleGroupsPython.
Mark Lawrence
More information about the Tutor
mailing list