Reputation: 119
I'm trying to create this program for class but I have a tiny issue. I always seem to have problems with loops! The program works if you enter the date as asked, but I can't get it to go back and ask the user for the date again if the input is wrong. I created the True/False boolean but it doesn't seem to work. We haven't learned how to import certain date modules and convert so I'm converting the months manually.
Instructions:
A. Input a date in numeric format from the user e.g. mm/dd/yy.
B. Examine the month entered by the user. If it is larger than 12 or smaller than 1 issue an error message and ask for input again.
C. Perform similar validation tests for the date and year. Year must be 2013. (Any other year is invalid). In addition, the year must only be two digits long.
D. Once all the input has been validated, output the string in long date format. Thus a string that was input at 06/01/15 will be output as June 1, 2015.
def main():
try_again = True
while try_again == True:
date_string = input("Please enter a date in the format 'mm/dd/yy': ")
date_list = date_string.split('/')
month = date_list[0]
day = date_list[1]
year = date_list[2]
if month > "12" or month < "01":
print('Error, please re-enter the date!')
if year != '13':
print('Error! The year must be 2013')
if len(year) > 2:
print('Error! The year can only be 2 digits long!')
else:
print_date(month, day, year)
try_again = False
def print_date(month, day, year):
# Convert to 2013 because 2013 is the only valid year
# for this program.
if year == '13':
year = '2013'
# Convert the number values to the actual month.
if month == '01':
month = 'January'
if month == '02':
month = 'February'
if month == '03':
month = 'March'
if month == '04':
month = 'April'
if month =='05':
month = 'May'
if month == '06':
month = 'June'
if month == '07':
month = 'July'
if month == '08':
month = 'August'
if month == '09':
month = 'September'
if month =='10':
month = 'October'
if month == '11':
month = 'November'
if month == '12':
month = 'December'
# Print the date in the correct format
print(month + " " + day + ',' + " " + year)
main()
Upvotes: 0
Views: 1868
Reputation: 21453
the else
is only on the last if
statement, either add continue
to each conditional or change the middle if
to elif
:
if month > "12" or month < "01":
print('Error, please re-enter the date!')
continue #back to beginning of loop
if year != '13':
print('Error! The year must be 2013')
continue #back to beginning of loop
if len(year) > 2:
print('Error! The year can only be 2 digits long!')
continue #back to beginning of loop
#else: #doesn't really change anything any more
#only happens if none of the continue s happend
print_date(month, day, year)
try_again = False
or with elif
s:
if month > "12" or month < "01":
print('Error, please re-enter the date!')
elif year != '13':
print('Error! The year must be 2013')
elif len(year) > 2:
print('Error! The year can only be 2 digits long!')
else:
print_date(month, day, year)
try_again = False
That way only one of the conditionals will happen.
Upvotes: 1