In the code below I am trying to get the second 'while' statement (while digit_check) to receive the new date from the earlier while_count statement. But it seems to be picking up the original assignment for the user_date variable from the first assignment line.
How can I get the new variable assignment to pass to the second while statement?
Thanks much
def main():
user_date = raw_input("Enter a date in the format mm/dd/yyyy and press 'Enter' ")
count = len(user_date)
digit = ''
digit_check = ''
w开发者_如何学Pythonhile count != 10:
user_date = raw_input('try again ')
count = len(user_date)
if user_date[0].isdigit() and user_date[1].isdigit() and user_date[3].isdigit() \
and user_date[4].isdigit() and user_date[6].isdigit() and user_date[7].isdigit() \
and user_date[8].isdigit() and user_date[9].isdigit() and user_date[2] == '/' \
and user_date[5] == '/':
digit_check = True
while digit_check != True :
user_date = raw_input('Not right - try again')
convert_date(user_date)
print 'That date is ',convert_date(user_date) + ' ' + user_date[3] + user_date[4] + ',' + user_date[6:]
def convert_date(user_date):
# Convert date to different format
month = ''
if user_date[0] == '0' and user_date[1] == '1':
month = 'January'
elif user_date[0] == '0' and user_date[1] == '2':
month = 'February'
elif user_date[0] == '0' and user_date[1] == '3':
month = 'March'
elif user_date[0] == '0' and user_date[1] == '4':
month = 'April'
elif user_date[0] == '0' and user_date[1] == '5':
month = 'May'
elif user_date[0] == '0' and user_date[1] == '6':
month = 'June'
elif user_date[0] == '0' and user_date[1] == '7':
month = 'July'
elif user_date[0] == '0' and user_date[1] == '8':
month = 'August'
elif user_date[0] == '0' and user_date[1] == '9':
month = 'September'
elif user_date[0] == '1' and user_date[1] == '0':
month = 'October'
elif user_date[0] == '1' and user_date[1] == '1':
month = 'November'
elif user_date[0] == '1' and user_date[1] == '2':
month = 'December'
return month
main()
One problem is that you aren't recalculating digit_check here:
while digit_check != True :
user_date = raw_input('Not right - try again')
This will just go into an infinite loop.
I'd suggest that instead of writing a huge function with many loops and lots of assignments, instead you refactor your code into smaller functions and use simpler logic. For example:
def getDate():
while True:
user_date = raw_input('Enter a date')
if validate(user_date):
return user_date
else:
print 'Error, try again.'
def validate(user_date):
# etc...
Or use the built-in library functions?
import re
import datetime
def getDate(msg="Enter a date in the format mm/dd/yyyy and press 'Enter': ", pat=re.compile(r'(\d{1,2})/(\d{1,2})/(\d{4})$')):
while True: # repeat until a valid date is entered
s = raw_input(msg) # get input
match = pat.match(s.strip()) # match against regular expression
if match: # match found?
m,d,y = match.groups()
try:
# parse to date
return datetime.date(int(y), int(m), int(d))
except ValueError:
# parsing failed (month 93 is invalid, etc)
pass
def main():
userDate = getDate()
print('You entered {0}'.format(userDate.strftime('%B %d, %Y')))
if __name__=="__main__":
main()
You aren't recomputing digit_check in that while loop, so it's failing forever, even though user_date is changing. Adding the if block inside the while loop after user_date = raw_input('Not right - try again') fixes it.
That being said, you should definitely make that check a separate function, especially since you'll now be calling it twice instead of once.
精彩评论