Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to design a Python program to both output a date in \"Month day, yea

ID: 3642551 • Letter: I

Question

I'm trying to design a Python program to both output a date in "Month day, year" format as well as decide whether the date itself is valid. I feel like this is a roundabout way (and it's not actually done) but this is what I have so far. Is there anything I could change or do better to get it to work?

ef main():
#get date
dateStr = input("Enter a date (mm/dd/yyyy): ")

#split into components
monthStr, dayStr, yearStr = dateStr.split("/")

#convert monthStr to the month name
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
monthStr = months[int(monthsStr)-1]

#check to see if date is valid
while True:
d = eval(input(dateStr))
if monthStr == 1 and dayStr <= 31:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Winter.")
elif monthStr == 2 and dayStr <=2 8:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Winter.")
elif monthStr == 3 and dayStr <= 31:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Winter.")
elif monthStr == 4 and dayStr <= 30:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Spring.")
elif monthStr == 5 and dayStr <= 31:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Spring.")
elif monthStr == 6 and dayStr <= 30:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Spring.")
elif monthStr == 7 and dayStr <= 31:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Summer.")
elif monthStr = 8 and dayStr <= 31:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Summer.")
elif monthStr = 9 and dayStr <= 30:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Summer.")
elif monthStr = 10 and dayStr <= 31:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Fall.")
elif monthStr = 11 and dayStr <= 30:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Fall.")
elif monthStr = 12 and dayStr <= 30:
print("The converted date is:", monthStr, dayStr+"," yearStr "and it is valid and in the Fall.")
else:
print("The date you entered was invalid.")


#output result in month day year format
print("The converted date is:", monthStr, dayStr+",", yearStr)

main()

Explanation / Answer

There were LOTS of errors in your code... I think the code below does what your are trying to do...

It does not include a loop, because I couldn't figure out what your while True: loop was doing. If it needs to loop, you'll have to add that.

Hope this helps!

==================== ==================== ==================== ====================

$ cat date.py

# Define full names of months

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

# Define number of days in each month

daysinmonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

# Define season for each month

seasons = ["Winter", "Winter", "Winter", "Spring", "Spring", "Spring", "Summer", "Summer", "Summer", "Fall", "Fall", "Fall"]

# prompt user to enter date in mm/dd/yyyy format

dateStr = raw_input("Enter a date (mm/dd/yyyy): ")

# split into components

dateSplit = dateStr.split("/")

month = int(dateSplit[0])

day = int(dateSplit[1])

year = int(dateSplit[2])

# convert month number (in month) to month name (save in monthStr)

monthStr = months[month-1]

# get the season

seasonStr = seasons[month-1]

#check to see if date is valid

if day <= daysinmonth[month-1]:

   print 'The converted date is: {0} {1}, {2} and it is in the {3}.'.format(monthStr, day, year, seasonStr)

else:

   print("The date you entered was invalid.")

==================== ==================== ==================== ====================

$ python date.py

Enter a date (mm/dd/yyyy): 12/21/2012

The converted date is: December 21, 2012 and it is in the Fall.

$ python date.py

Enter a date (mm/dd/yyyy): 2/31/2012

The date you entered was invalid.

$