0% found this document useful (0 votes)
49 views

Computing Key Stage 3 Lesson COMy9u5L1

This document provides instructions and code snippets for Python programs that work with lists of months and seasons. The programs prompt the user to input a month number and use that to display the corresponding month name or season from a pre-defined list. The tasks guide the reader to complete partial code by indexing into lists and using Boolean expressions to check month numbers and assign the appropriate season. An optional task replaces user input with code to retrieve the current month automatically.

Uploaded by

Aruj Tahir Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Computing Key Stage 3 Lesson COMy9u5L1

This document provides instructions and code snippets for Python programs that work with lists of months and seasons. The programs prompt the user to input a month number and use that to display the corresponding month name or season from a pre-defined list. The tasks guide the reader to complete partial code by indexing into lists and using Boolean expressions to check month numbers and assign the appropriate season. An optional task replaces user input with code to retrieve the current month automatically.

Uploaded by

Aruj Tahir Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Computing

Lesson 1: Warm up

Python programming with sequences of data

Rebecca Franks

Materials from the Teach Computing Curriculum created by the National Centre for Computing Education

1
How long until the weekend?

2
Syntax checklist Error message support

If you are faced with an error message, read it carefully and try to fix the problem.

Use the list below to check for common errors:

misspelled if, elif, or else (this includes using capitals)


forgot the colon : at the end of a line containing if, elif, or else
neglected to indent statements in the if–block, elif–block, or else–block
indented if, elif, or else by mistake
used = instead of == in a condition, to check if two values are equal
used quotes around the name of a variable
forgot to use quotes around a string literal (like "Monday")

3
Task 1 How long until the weekend?
Step 1

Open this incomplete program oaknat.uk/comp-py-week-2 in Repl.it:

1 print("What day is it today?")


2 day = int(input())
3 if day <= 4:
4 print("It’s a weekday")
5 remaining = .
6 else:
7 print("It’s the weekend!")

Remember: The program uses an integer for each day of the week, ranging from 0 for Monday
to 6 for Sunday. Here’s a handy reference:

0 1 2 3 4 5 6

4
Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Task 1 How long until the weekend?
Step 2

Complete line 5, so that the value of remaining is the number of days left until the weekend
(including the current day).

Tip: The current day is an integer, ranging from 0 to 4 in the case of weekdays. Saturday is day 5.

Step 3

Insert the line below in your program, wherever you believe it is appropriate to display the value
of remaining days to the user.
print(remaining, "days until the weekend")

Tip: Make sure this message is displayed only in the case that the day is a weekday.

5
Task 1 How long until the weekend?

Example input and output


Note: Use this example to check your program. Given the input you see in this
sample interaction, this is the output your program should produce.
The program displays a prompt and What day is it today?
waits for keyboard input.

The user types in a reply. 1

The program identifies the type of day It’s a weekday


and displays the days remaining until the 4 days until the weekend
weekend.

6
Task 1 How long until the weekend?

Step 4

Extend the program so that Friday (day 4) is treated differently, with a different message
displayed than for the rest of the weekdays.

Tip: You will need to use if-elif-else, since there will now be three branches in your program.

Example input and output for this can be seen on the next slide.

7
Task 1 How long until the weekend?

Example input and output


Note: Use this example to check your program. Given the input you see in this sample interaction, this
is the output your program should produce.
The program displays a prompt What day is it today?
and waits for keyboard input.

The user types in a reply. 4

The program identifies the It’s Friday


particular day and displays a Just a day left until the weekend
custom message.

8
Explorer Task (Optional task)

Instead of asking the user to type the current day of the week, the program can retrieve the
actual day, using the datetime module.

Replace the lines that receive user input (marked with -) with the lines that retrieve the current
day of the week (marked with +).

- print("What day is it today?")


- day = int(input())
+ from datetime import datetime
+ day = datetime.now().weekday()

9
As seasons roll on by

10
Worked Example 1

This program accesses a list called days, which contains the names of days, to display two
specific items (those with index 5 and 6).

1 days = ["Monday", "Tuesday",


2 "Wednesday", "Thursday",
3 "Friday", "Saturday", "Sunday"]
4 print("These are the weekend days:")
5 print(days[5])
6 print(days[6])

11
Worked Example 2 .
This program accesses a list called days, which contains the names of days, to display the name
of the current day and the day before that.

12
Task 1 Summer
Step 1

Open this incomplete program oaknat.uk/comp-py-months-1 in Repl.it:

1 months = ["January", "February",


2 "March", "April", "May",
3 "June", "July", "August",
4 "September", "October", "November",
5 "December"]
6 print("These are the summer months:")
7 print(months[ ])
8 print(months[ ])
9 print(months[ ])

13
Task 1 Summer
Step 2

Complete lines 7, 8, and 9 with an integer, so that the program displays the names of the
summer months.

Tip: You are used to numbering months starting from 1 for January. However, list items in
Python are numbered starting from 0. You need to take that into account.

Example
Note: Use this example to check your program. This is the output your program should produce.

The program displays the names of the These are the summer months:
summer months. June
July
August

14
Task 2 Name for a number
Step 1

Open this incomplete program oaknat.uk/comp-py-months-3 in Repl.it:

1 months = ["January", "February",


2 "March", "April", "May",
3 "June", "July", "August",
4 "September", "October", "November",
5 "December"]
6 print("What month is it? (1-12)")
7 month = int(input())
10 print("It is", months[ ])

15
Task 2 Name for a number
Step 2

Complete line 10 with an expression, so that the program displays the name of the current
month.

Tip: You are used to numbering months starting from 1 for January. However, list items in
Python are numbered starting from 0. You need to take that into account.

Example
Note: Use this example to check your program. Given the input you see in this sample interaction, this is the
output your program should produce.
The program displays a prompt and waits for What month is it? (1-12)
keyboard input.

The user types a reply. 1

The program displays the name of the month. It is January


16
Task 2 Name for a number
A further example...

Example
Note: Use this example to check your program. Given the input you see in this sample interaction, this is
the output your program should produce.
The program displays a prompt and waits What month is it? (1-12)
for keyboard input.

The user types a reply. 12

The program displays the name of the It is December


month.

17
Task 3 Seasons
Step 1

Open this incomplete program oaknat.uk/comp-py-months-5 in Repl.it:

1 seasons = ["Winter", "Spring",


2 "Summer", "Autumn"]
3 print("What month is it? (1-12)")
4 month = int(input())
5 if :
6 season = 0
7 elif :
8 season = 1
9 elif :
10 season = 2
11 else:
12 season = 3

18
13 print("It is", seasons[season])
Task 3 Seasons
Step 2

Complete lines 5, 7, and 9 with a Boolean expression (a condition) that checks the value of the
month variable and assigns an appropriate value to season.

Tip: The numbering of seasons is arbitrary. Looking at the seasons list, numbering ranges from
0 for winter to 3 for autumn.

Tip: The condition required in line 5 should check the month variable, to determine if it
corresponds to a winter month (season 0). The numbers for winter months are not consecutive
(1, 2, and 12), so you will need to use or in the condition.

19
Explorer Task (optional)
Instead of asking the user to type the current month, the program can retrieve the actual
month, using the datetime module.

Replace the lines that receive user input (marked with -) with the lines that retrieve the current
month (marked with +).

- print("What month is it? (1-12)")


- month = int(input())
+ from datetime import datetime
+ day = datetime.now().month

20

You might also like