[Tutor] Breaking out of loop...
Albert-Jan Roskam
fomcl at yahoo.com
Fri Nov 20 18:55:32 CET 2009
Hi!
Slightly different (extented) than your original question, but here's how I'd do this (although the program doesn't really do very much):
import time, random
def is_valid_date():
while True:
prompt = 'Enter the date in ISO format (YYYY-MM-DD), or 0 to exit: '
date = raw_input(prompt)
try:
time.strptime(date, "%Y-%m-%d")
print "You've entered: %s" % date
return True
except ValueError:
if date == str(0):
print "Finished!"
break
else:
print "Invalid date or invalid date format (%s)" % date
return False
def do_something():
for i in range(10):
print "%05d" % random.randint(0, 10**4)
if is_valid_date():
do_something()
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- On Fri, 11/20/09, Ken G. <beachkid at insightbb.com> wrote:
From: Ken G. <beachkid at insightbb.com>
Subject: [Tutor] Breaking out of loop...
To: tutor at python.org
Date: Friday, November 20, 2009, 6:02 PM
I am trying to break out of a loop posted below. When asked for monthdate, it should break out if I entered the number zero and it does not. GRRR. Been working on this for almost an hour.
monthdate = 999
while monthdate <> 0:
monthdate = raw_input('Enter the month and date in two digit format each: ') month = monthdate[0:2]
date = monthdate[2:4]
year = ('2009')
print month, date, year
print "Finished!"
Thanking you all in advance for your help.
Ken
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091120/af589f0b/attachment.htm>
More information about the Tutor
mailing list