Need Help fixing this in Python, I can not get it to work. from datetime import
ID: 3690229 • Letter: N
Question
Need Help fixing this in Python, I can not get it to work.
from datetime import datetime
print ("Python code to print out Date as a long data format")
ipDate1 = input (' Please enter date in format mm/dd/yy: ')
month = ipDate1[2:]
month1 = int(month)
print(month1)
while((month1 <1 ) & (month1 > 12 )) : # then it is wrong keep reading
if int(month1) < 1:
print ( "Error , Month cannot be < 1, please re enter a valid month")
if int(month1) > 12:
print ( "Error , Month cannot be > 12, please re enter a valid month")
ipDate1 = input (' Please enter date in format mm/dd/yy: ')
day = ipDate1[4:]
day1 = int(day)
print(day1)
while((day1 <1 ) & (day1 > 31 )) : # then it is wrong keep reading
if int(day1) < 1:
print ( "Error , Day cannot be < 1, please re enter a valid Day")
if int(day1) > 31:
print ( "Error , Day cannot be > 31, please re enter a valid Day")
ipDate1 = input (' Please enter date in format mm/dd/yy: ')
year = ipDate1[-2:]
year1 = int(year)
if ( year1 != 13):
print ("Error, year cannot be < 13 and cannot be > 13, please reenter year")
ipDate1 = input (' Please enter date in format mm/dd/yy: ')
# convert the input date read in as a string into the datetime pattern as per the python built in object
ipDate2 = datetime.strptime(ipDate1, "%m/%d/%y")
# now print out the date in the long format
print (ipDate2.strftime("%B %d, %Y"))
Explanation / Answer
Corrected code:
from datetime import datetime
print ("Python code to print out Date as a long data format")
ipDate1 = raw_input (' Please enter date in format mm/dd/yy: ')
month = ipDate1[:2]
month1 = int(month)
print(month1)
while((month1 <1 ) & (month1 > 12 )) : # then it is wrong keep reading
if int(month1) < 1:
print ( "Error , Month cannot be < 1, please re enter a valid month")
if int(month1) > 12:
print ( "Error , Month cannot be > 12, please re enter a valid month")
ipDate1 = raw_input (' Please enter date in format mm/dd/yy: ')
day = ipDate1[3:5]
day1 = int(day)
print(day1)
while((day1 <1 ) & (day1 > 31 )) : # then it is wrong keep reading
if int(day1) < 1:
print ( "Error , Day cannot be < 1, please re enter a valid Day")
if int(day1) > 31:
print ( "Error , Day cannot be > 31, please re enter a valid Day")
ipDate1 = input (' Please enter date in format mm/dd/yy: ')
year = ipDate1[-2:]
year1 = int(year)
if ( year1 != 13):
print ("Error, year cannot be < 13 and cannot be > 13, please reenter year")
ipDate1 = raw_input (' Please enter date in format mm/dd/yy: ')
# convert the input date read in as a string into the datetime pattern as per the python built in object
ipDate2 = datetime.strptime(ipDate1, "%m/%d/%y")
# now print out the date in the long format
print (ipDate2.strftime("%B %d, %Y"))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.