Reputation: 13
I am writing a script to ask user to enter a date. If it is a date format, then return the entry; otherwise, continue. But my code doesn't stop even the user input is valid. Could somebody please share some insight? Thanks!
def date_input(prompt):
while True:
date_text = raw_input(prompt)
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
return date_text
except:
print('Invalid input.')
continue
Upvotes: 1
Views: 71
Reputation: 117661
You should never use except
, always check for a specific exception:
except ValueError:
Then your real error should come through. I suspect you didn't import datetime
.
Upvotes: 2