What season is this month in?
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
match
Provides an alternative structure to `if, elif, else` and is used in situations where there are more than two possibilities. There are also more advanced uses of match that make it more useful than `if`. You need to be using Python 3.10 or higher to use the match command. Don't forget the colon at the end of the match statement.
case
Is the value for the match command. Possibilities for the value can be separated with pipe characters: `|`. You can have as many case statements as you need. Don't forget the colon at the end of the case statement.
case _
An underscore is a special condition that is the same as `else`. It captures anything else that was not matched by the previous case statements. Don't forget the colon at the end of the case statement.
Questions to think about with this program to check your understanding:
In line 25: `print("Month", month, "is in the", season)` why is the word month written twice in this line, but it is only output once?
"Month" in double quotes is a string, it is the actual word, "Month". In contrast, month without the double quotes is a variable: an area in memory to hold data that can change. This is holding the name of the month the user inputs. E.g. "May". As we want the word "Month" followed by the name of the month to be output we need both in the statement.
How could line 8 be written using an `if` instead of `case`?
if month == "December" or month == "January" or month == "February":
Change the program so that:
Enter the number of the month: 5
Month 5 is in the Spring
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
Enter the month: 1
Month 1 is in the Winter
Enter the month: September
Month September is in the Autumn
Enter the month: 8
Month 8 is in the Summer