[Python] ZyBooks Lab:Seasons assignment - What's wrong with my code

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    Lab:Seasons assignment Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day.

    Ex: If the input is:

    April 11 the output is:

    Spring In addition, check if the string and int are valid (an actual month and day).

    Ex: If the input is:

    Blue 65 the output is:

    Invalid The dates for each season are: Spring: March 20 - June 20 Summer: June 21 - September 21 Autumn: September 22 - December 20 Winter: December 21 - March 19

    my code so far:

    input_month = input()
    input_day = int(input())

    months = ('January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December')
    if not (input_month in months):
    print ('Invalid')

    elif input_month == 'January':
    if not (0 < input_day <= 31):
    print('Invalid')
    else:
    print('Winter')

    elif input_month == 'February':
    if not (0 < input_day <= 29):
    print('Invalid')
    else:
    print('Winter')

    elif input_month == 'March':
    if (0 < input_day < 20):
    print('Winter')
    elif (20 <= input_day <= 31):
    print('Spring')
    else:
    print('Invalid')

    elif input_month == 'April':
    if not (0 < input_day <= 30):
    print('Invalid')
    else:
    print('Spring')

    elif input_month == 'May':
    if not (0 < input_day <= 31):
    print('Invalid')
    else:
    print('Spring')

    elif input_month == 'June':
    if (0 < input_day <= 20):
    print('Spring')
    elif (20 < input_day <= 30):
    print('Summer')
    else:
    print('Invalid')

    elif input_month == 'July' or 'August':
    if not (0 < input_day <= 31):
    print('Invalid')
    else:
    print('Summer')

    elif input_month == 'September':
    if not(0 < input_day <= 30):
    print("Invalid")
    elif (0 < input_day <= 21):
    print ("Summer")
    else:
    print ("Autumn")

    elif input_month == 'October':
    if not (0 < input_day <= 31):
    print('Invalid')
    else:
    print('Autumn')

    elif input_month == 'November':
    if not (0 < input_day <= 30):
    print('Invalid')
    else:
    print('Autumn')

    elif input_month == 'December':
    if (0 < input_day <= 20):
    print('Autumn')
    elif (20 < input_day <= 31):
    print('Winter')
    else:
    print('Invalid')


    I am getting errors for September 31 and November 7th and I can't figure out why?

    Continue reading...

Compartilhe esta Página