Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

My loops aren't executing like I thought they would

+4 votes
asked Mar 18, 2022 by Joey (540 points)

This is the information that I was given:

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

This is what I have coded so far:

# Input the month and day
input_month = input()
input_day = int(input())
month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
    'September', 'October', 'November', 'December']

#TODO: write if-else statements to determine if the input is valid. Output the result if input is not valid.
if input_month not in month or input_day > 31:
    print('Invalid')

#TODO: write if-else statements to test each month and determine the season of the corresponding date. Output the season.
#Note: pay special care on March, June, September, and December for two possible seasons
if input_month == ('December') and input_day > 20:
    if input_month == ('January') and input_day < 32:
        if input_month == ('February') and input_day < 29:
            if input_month == ('March') and input_day < 20:
                print('Winter')
            
elif input_month == ('March') and input_day > 19:
    if input_month == ('April') and input_day < 31:
        if input_month == ('May') and input_day < 32:
            if input_month == ('June') and input_day < 21:
                print('Spring')

elif input_month == ('June') and input_day > 20:
    if input_month == ('July') and input_day < 32:
        if input_month == ('August') and input_day < 32:
            if input_month == ('September') and input_day < 22:
                print('Summer')

elif input_month == ('September') and input_day > 21:
    if input_month == ('October') and input_day < 32:
        if input_month == ('November') and input_day < 31:
            if input_month == ('December') and input_day < 21:
                print('Autumn')

I don't understand what is going wrong? A nudge in the right direction would be GREATLY appreciated

2 Answers

0 votes
answered Mar 18, 2022 by Sam (1,310 points)
I'm not sure what the output is but when you had month on two lines you need a backslash "\" or in python-3 a line continuation character
0 votes
answered Mar 19, 2022 by Peter Minarik (84,720 points)

Last time you mentioned you don't want a ready solution, just a nudge in the right direction. Hence I won't try to run your code, just look at it and make comments based on what I notice. :)

Problem 1

When the input is deemed invalid, you print invalid, but your program keeps running. You should only check the months if the input was valid. (else branch or sys.exit() command).

Problem 2

When you check for a season, you check if a month is e.g. December, If it is, you check if the month is January. But that's impossible. A month can only be either December or January.

You should check if a month is December and a day is at least 20, or any day in January or any day in February or a March with a day lower than 20.

Note

My loops aren't executing like I thought they would

Your code doesn't contain any loops (while and for are loops).

Good luck!

commented Mar 20, 2022 by Joey (540 points)
edited Mar 20, 2022 by Joey
Ok so if I'm understanding this right you're saying that I need to iterate through the months with maybe a for loop (since I know that there are only 4 months max to choose from per season) so maybe:
#gonna use python appropriate grammer to try and be exact as possible so you can understand what I'm doing as I go through
for month in months #I'll change month to months so I have a container
\t if month in season end='' #maybe divide them all into a season container?  
and day #comparison operator, dayInteger
print(season)
Or am I off the mark again?

Edit: I really appreciate the nudge instead of the outright answer as well lol. Sometimes the answer is so simple that there is no explanation but something like this I really appreciate the chance to learn it from a peer so Thank you :)

Edit2: Maybe a dictionary for the seasons and include the months as values and the seasons themselves as keys? I'm working on the code while I wait on the response and the ideas just keep coming lol *smol revision XD maybe not a dictionary*
commented Mar 20, 2022 by Peter Minarik (84,720 points)
I didn't say you need to iterate through months or you need a loop. My *note* was about your topic talking about a "loop" but your code does not have one. It doesn't need one though. :)

I'll write some pseudo-code of how I'd do it, to make it clearer:

IF date.month == December AND date.day >= 21 OR
    date.month == January OR
    date.month == February OR
    date.month == Match AND date.day <= 19
THEN
    itIsWinter
ELSE
    checkForOtherSeasons

I hope this makes sense now.

Good luck!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...