Oxford Maths Victorian Curriculum Year 8 Sample Chapter

Page 1

SAMPLE CHAPTER UNCORRECTED PAGE PROOFS To learn more about the Oxford Maths 7–10 series, visit:

oup.com.au/maths

For more information, or to book an appointment with your local sales consultant, contact: Russell Comb Email: russell.comb@oup.com | Mobile: 0423 241 455 Alicia Giaquinta Email: alicia.giaquinta@oup.com | Mobile: 0411 759 611 David Griffiths Email: david.griffiths@oup.com | Mobile: 0411 759 659 Dave Presser Email: dave.presser@oup.com | Mobile: 0427 225 891

MATHEMATICS

OXFORD

MA TH S8 S E R I E S C O N S U LTA N T: THOMAS CHRISTIANSEN HELEN SORENSON ALEX ANDER BL ANKSBY EUGENE ROIZMAN JENNIFER NOL AN MEL ANIE KOETSVELD S O N J A S TA M B U L I C ROBERT BELL

V I C T O R I A N C U R R I C U L U M


FT A D R No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 00_OM_VIC_Y7_SB_29198_TXT_PRE_1PP.indd 4

17-Feb-21 09:26:58


Contents Using Oxford Maths 8 for the Victorian Curriculum ���������................������ xxx Chapter 1 Integers ������������������������������������������������������������������������������������������������ xxx 1A Rounding and estimating �������������������������������������������������������������������������������������������� xxx 1B Adding and subtracting whole numbers ��������������������������������������������������������������������� xxx 1C Multiplying and dividing whole numbers �������������������������������������������������������������������� xxx 1D Multiples, factors, squares and cubes �������������������������������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 1E Negative integers ����������������������������������������������������������������������������������������������������������� xxx 1F Adding and subtracting integers ���������������������������������������������������������������������������������� xxx 1G Multiplying and dividing integers ��������������������������������������������������������������������������������� xxx 1H Order of operations ������������������������������������������������������������������������������������������������������� xxx

T

Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Chapter 2 Fractions and decimals ���������������������������������������������������������������������� xxx

AF

2A Fractions ������������������������������������������������������������������������������������������������������������������������� xxx 2B Adding and subtracting fractions �������������������������������������������������������������������������������� xxx 2C Multiplying and dividing fractions �������������������������������������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 2D Decimals ������������������������������������������������������������������������������������������������������������������������ xxx

R

2E Adding and subtraction decimals �������������������������������������������������������������������������������� xxx 2F Multiplying and dividing decimals �������������������������������������������������������������������������������� xxx 2G Terminating, non-terminating and recurring decimals ����������������������������������������������� xxx

D

Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Chapter 3 Percentages, ratios and rates ������������������������������������������������������������� xxx 3A Percentages, decimals and fractions��������������������������������������������������������������������������� xxx 3B Percentages calculations ���������������������������������������������������������������������������������������������� xxx 3C Financial calculations ��������������������������������������������������������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 3D Ratios ������������������������������������������������������������������������������������������������������������������������������ xxx 3E Equivalent ratios ������������������������������������������������������������������������������������������������������������� xxx 3F Dividing a quantity in a given ration����������������������������������������������������������������������������� xxx 3G Rates ������������������������������������������������������������������������������������������������������������������������������ xxx Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means.


Chapter 4 Indices and roots ����������������������������������������������������������������������������������������� xxx 4A Indices ���������������������������������������������������������������������������������������������������������������������������� xxx 4B Multiplying and dividing numbers with the same base ���������������������������������������������� xxx 4C Raising indices and the zero index ������������������������������������������������������������������������������ xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 4D Combining the index laws �������������������������������������������������������������������������������������������� xxx 4E Roots ������������������������������������������������������������������������������������������������������������������������������� xxx Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Semester 1 review������������������������������������������������������������������������������������������������������������� xxx AMT problem set��������������������������������������������������������������������������������������������������������������� xxx Chapter 5 Algebra����������������������������������������������������������������������������������������������������������� xxx 5A Pronumerals ������������������������������������������������������������������������������������������������������������������� xxx 5B Substitution ��������������������������������������������������������������������������������������������������������������������� xxx

T

5C Adding and subtracting algebraic terms �������������������������������������������������������������������� xxx 5D Multiplying algebraic terms ������������������������������������������������������������������������������������������ xxx

AF

Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 5E Dividing algebraic terms ������������������������������������������������������������������������������������������������ xxx 5F Expanding����������������������������������������������������������������������������������������������������������������������� xxx 5G Factorising ��������������������������������������������������������������������������������������������������������������������� xxx Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

R

Chapter 6 Linear relationships �������������������������������������������������������������������������������������� xxx 6A Equations ����������������������������������������������������������������������������������������������������������������������� xxx

D

6B Solving equations using inverse operations ����������������������������������������������������������������� xxx 6C Solving equations with the unknown on both sides ��������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 6D Plotting linear and non-linear relationships ����������������������������������������������������������������� xxx 6E Solving linear equations using graphs ������������������������������������������������������������������������� xxx 6F Finding linear equations ������������������������������������������������������������������������������������������������ xxx Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Chapter 7 Congruence�������������������������������������������������������������������������������������������������� xxx 7A Angles����������������������������������������������������������������������������������������������������������������������������� xxx 7B Triangles �������������������������������������������������������������������������������������������������������������������������� xxx 7C Transformations �������������������������������������������������������������������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 7D Congruence ������������������������������������������������������������������������������������������������������������������ xxx

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means.


7E Congruent triangles ������������������������������������������������������������������������������������������������������� xxx 7F Constructing triangles���������������������������������������������������������������������������������������������������� xxx 7G Quadrilaterals ���������������������������������������������������������������������������������������������������������������� xxx Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Chapter 8 Units of measurement ��������������������������������������������������������������������������������� xxx 8A Length and perimeter ��������������������������������������������������������������������������������������������������� xxx 8B Circumference of a circle ���������������������������������������������������������������������������������������������� xxx 8C Area of triangles and rectangles���������������������������������������������������������������������������������� xxx 8D Area of quadrilaterals ���������������������������������������������������������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx 8E Area of a circle ��������������������������������������������������������������������������������������������������������������� xxx 8F Volume of prisms ������������������������������������������������������������������������������������������������������������ xxx 8G Time �������������������������������������������������������������������������������������������������������������������������������� xxx

T

Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Chapter 9 Statistics���������������������������������������������������������������������������������������������������������� xxx

AF

9A Collecting data and sampling methods ��������������������������������������������������������������������� xxx 9B Summary statistics ��������������������������������������������������������������������������������������������������������� xxx 9C Presenting data ������������������������������������������������������������������������������������������������������������� xxx 9D Analysing data��������������������������������������������������������������������������������������������������������������� xxx Checkpoint �������������������������������������������������������������������������������������������������������������������������� xxx

R

9E Theoretical probability ���������������������������������������������������������������������������������������������������� xxx 9F Two-way tables ���������������������������������������������������������������������������������������������������������������� xxx 9G Venn diagrams �������������������������������������������������������������������������������������������������������������� xxx

D

Chapter review �������������������������������������������������������������������������������������������������������������������� xxx

Chapter 10 Computational thinking ��������������������������������������������������������������������������� xxx 10A Lists ������������������������������������������������������������������������������������������������������������������������������� xxx 10B Loops ���������������������������������������������������������������������������������������������������������������������������� xxx 10C Applications of loops �������������������������������������������������������������������������������������������������� xxx

Semester 2 review������������������������������������������������������������������������������������������������������������� xxx AMT problem set��������������������������������������������������������������������������������������������������������������� xxx STEAM���������������������������������������������������������������������������������������������������������������������������������� xxx Answers ����������������������������������������������������������������������������������������������������������������������������� xxx Glossary ����������������������������������������������������������������������������������������������������������������������������� xxx Index ����������������������������������������������������������������������������������������������������������������������������������� xxx

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means.


10 D

R

AF

T

Computational Thinking

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 2

20-Apr-21 20:09:33


Index 10A Lists 10B Introduction to loops 10C Application of loops

Prerequisite skills Before starting this chapter, please ensure you have completed the Computational Thinking chapter from Oxford Maths 7. Diagnostic pre-test Take the pre-test to make sure you’re ready for this chapter!

Interactive skill sheets Complete these skill sheets to develop the prerequisite skills for this chapter.

T

Curriculum links

AF

• Use algorithms and related testing procedures to identify and correct errors (VCMNA282) © VCAA

Materials

D

R

✔ Computer ✔ Python (online or downloaded) Note: Python is used as the coding language in this chapter. Instructions will vary for other coding languages.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 3

20-Apr-21 20:09:36


10A Lists Learning intentions

Inter-year links

✔ I can define a list and look up specific items in a list

Year 7 11A Printing information to the computer screen

✔ I can change elements in a list and add items to the end of a list ✔ I can determine the length of a list

Year 9

10A Nested loops

Year 10

12A Two-dimensional lists

Lists • •

A list is an ordered collection of items. These items can be almost any type of data, for example shopping lists or a list of favourite TV shows, but in this chapter we will focus primarily on numbers. A list is defined similarly to any other variable.

T

Square brackets on either side indicate a list

AF

L = [5, 2, 7, 4, 9]

Commas are used to separate each item in the list

R

The position of each item in the list is called its index. The index of a list starts at 0 rather than 1. To access a specific item in a list, the following syntax is used: L[2]

D

The name of the list

L = [5,    2,   7,    4,   9] Index: 0  1  2  3  4

Square brackets around the index of the item

The index of the item that is being accessed

The length of a list can be found using the syntax len().

Changing a list •

There are many ways to change a list. In this chapter we will introduce two of them. ➝ To change an element in an existing list, the following syntax is used: L[2] = 3 The name of the list

The new value of the item

The index of the item that is being changed

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 4

20-Apr-21 20:09:37


➝ To add an element to the end of a list, the following syntax is used: L = L + [3] The name of the list

The item being added to the end of the list

Example 10A.1  Defining a list and checking its length a Define a list named L that contains the first 5 even numbers. b Print the list. c Check the length of the list using the len()command. WRITE

a L = [2, 4, 6, 8, 10] b print(L) c print(len(L))

AF

a Write the variable name first, followed by an equals sign, and then the list itself. b Use the print()command to print the list. c Use len()to print the length of the list.

T

THINK

Example 10A.2  Accessing elements in a list Consider the list:

State the index of 4 in L. What element is at index 0? What will print(L[3])display on the screen? What will print(L[5])display on the screen?

D

a b c d

R

L = [2,4,6,8,10]

THINK

a The index of the first item is 0, not 1, so ensure you start counting from 0. b Index 0 refers to the first item in the list. c L[3]refers to the item at index 3 d L[5]refers to the item at index 5; however, the last item in the list is at index 4 so there is no index 5 for this list! This will cause an error.

WRITE

a 1 b 2 c 8 d An error message will be displayed on the screen as no item with index 5 exists in this list. The error message will be IndexError: list index out of range.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 5

20-Apr-21 20:09:37


Example 10A.3  Changing a list Consider the following list. L = [2, 3, 5, 7, 11, 13, 17] a Write a line of code that will change the 5 in L to 25. b Write a line of code that will add 23 to the end of L. c The following code was written by a student. What are the contents of L at the completion of the student’s code? L[2] = 8 L = L + [31] L[1] = 2 L = L + [4] L[8] = 7 WRITE

a L[2] = 25 b L = L + [23]

c [2, 2, 8, 7, 11, 13, 17, 31, 7]

AF

a The 5 in L is at index 2. To access index 2 use L[2]. b To add an item to the end of a list use the + operation. c Consider the code line by line: After line 1: [2, 3, 8, 7, 11, 13, 17]

T

THINK

After line 2: [2, 3, 8, 7, 11, 13, 17, 31]

R

After line 3: [2, 2, 8, 7, 11, 13, 17, 31]

After line 4: [2, 2, 8, 7, 11, 13, 17, 31, 4]

D

After line 5: [2, 2, 8, 7, 11, 13, 17, 31, 7]

Helpful hints

✔ When working with lists, it is common to accidentally try to access an item that doesn’t exist (by using an index that is too high). If you do, the computer will report an error. The error will end with the line: IndexError: list index out of range ✔ When adding an item to the end of a list, make sure the new item is inside square brackets.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 6

20-Apr-21 20:09:37


EXERCISE 10A Lists 1–2

10A.1

1–4

1–5

1 For each of the following lists: i Write a line of code defining the list. ii Write down their length. a A list of the first 10 even numbers. b A list of every multiple of 6 between 1 and 50. c A list of every subject you study at school.

10A.2

2 Consider the list L = [1, 1, 2, 3, 5, 8, 13, 21] b Which item is at index 3?

c What will print(L[2])display?

d What will print(L[10])display?

3 Consider the list

T

a What is the index of 13 in the list?

P = [ "Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Jigglypuff"] c What will print(P[1])display? 10A.3

4 Consider the list defined by

b Which item is at index 4?

AF

a What is the index of "Bulbasaur"?

d What will print(P[5])display?

L = [7, 22, 11, 34, 17, 52, 26, 13, 40] a What is the index of the element 26?

b Which item is at index 2?

c What will print(L[4])display?

d What will print(L[7])display?

R

5 Consider the list defined by

L = [20, 30, 50, 60]

D

a A student would like to change the list to be [10, 30, 50, 70, 90, 110]. Write down four lines of code that, in order, would be able to achieve these changes. b Another student writes the following code to change the original list. Write down the updated list after each line of code below: L[1] = 40 L = L + [100] L[3] = 80 L[2] = 60 L = L + 120

Check your student obook for these digital resources and more Interactive worksheet Lists

Investigation Lists in coding

Go further Year 9: 10A Nested loops

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 7

20-Apr-21 20:09:38


10B Introduction to loops Learning intentions

Inter-year links

✔ I can use a while loop to repeat a set of instructions as long as a condition is met

Year 7 11A Printing information to the computer screen

✔ I can use a for loop to iterate through a list of items

Year 9

10A Nested loops

Year 10

12A Two-dimensional lists

Loops

While loops • •

T

A loop describes a set of instructions that is repeated until a stopping condition is reached. Two types of loops will be covered in this chapter: ➝ While loops: a loop that repeats as long as a certain condition is true. ➝ For loops: a loop that repeats instructions for each item in a collection of items. Loops are one of the most crucial ideas in coding, as they allow the coder to tell a computer to repeat the same action over and over, without having to repeat the code. This can save the coder from writing thousands of lines of additional code!

AF

• •

A while loop can be thought of as an extension of the if statement that was covered in Year 7 (11D Conditions in code). Consider an if statement and a while loop that are concerned with a similar idea:

R

if it is raining:

the condition in the if statement is checked once and if it is true, the indented instruction below is performed

D

bring your umbrella

while it is raining:

the condition in the while loop is checked repeatedly and as long as it's true, the indented instruction below is performed. As soon as the condition is false, the while loop is exited.

hold up your umbrella

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 8

20-Apr-21 20:09:38


An analogy for understanding how a while loop works is the scenario of a family driving to a holiday destination, with the young child in the backseat repeatedly asking “are we there yet?”. Within the while loop structure, this could be written as: condition is checked repeatedly as long as it’s true

while car not arrived: each of the indented lines below are run and at the end of the last line we return to the start of the while loop

young child asks "are we there yet?"

parent reply "not yet!"

AF

T

➝ We can also use a diagram to represent this situation:

while car has arrived:

R

young child asks "are we there yet?"

D

parents reply "not yet!"

➝ Tracing the arrows on the diagram above with your finger, you will note that the arrows form a loop, and this is a good way to remember that a loop in coding suggests repeating a set of instructions over and over.

For loops • •

A for loop repeats a set of instructions over a specified number of iterations (or repetitions) based on the number of items in a collection. Consider the scenario of a teacher wanting to hand out a worksheet to each student in the class. This could be written using a for loop as follows: for each student in the class: give the student a worksheet

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 9

20-Apr-21 20:09:39


Without a loop, the instruction would read: give the first student a worksheet give the second student a worksheet (…and so on until…) give the twenty-fifth student a worksheet In coding, there is no need for words such as each, the or a, so our for loop would read: for student in class: give student worksheet

student here is a variable known as dummy variable

For loops will generally have the structure: for variable in list: command

T Maths ​a  =  b​

Python ​a= =b​

Not equals

​a  ≠  b​

​a!=b​

R

AF

Condition Equals Less than

​a  <  b​

​a<b​

Less than or equal to

​a  ≤  b​

​a<=b​

Greater than

​a  >  b​

​a>b

Greater than or equal to

​a  ≥  b​

​a>=b​

D

➝ The working of a for loop can be shown using a table. In this example the dummy variable changes to the next item of the list with each iteration of the loop. Iteration 1 2 3 4 5 for prime in [2, 3, 5, 7, 11]: 2 3 5 7 11 prime print(prime) When using for loops, it is often necessary to generate a certain range of integers. The syntax for i in range(x): is used to generate every integer from 0 to x − 1 (x itself is not included). For example, for i in range(5): is identical to for number in [0,1,2,3,4].

EXAMPLE 10B.1  Reading while loops For each of the following while loops, write down what will be printed on the screen. a n = 4 while n > 0: print(n) n = n – 1 b L = ["apple", "banana", "cherry", "dragonfruit"] i = 0 while i <= 3: print(L[i]) i = i + 1

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 10

20-Apr-21 20:09:39


THINK

a 1 Line 1 defines n as a variable with a value of 4. 2 Line 2 initiates a while loop that will repeat Lines 3 and 4 as long as the value of n is greater than 0. 3 Line 3 prints the current value of n. 4 Line 4 decreases the value of n by 1. The following table tracks the progress of the while loop: while condition 4 > 0 (TRUE) 3 > 0 (TRUE) 2 > 0 (TRUE) 1 > 0 (TRUE) 0 > 0 (FALSE)

Iteration Printed on screen 1 4 2 3 3 2 4 1 NO FURTHER ITERATIONS

Value of n (updated) ​4 − 1  =  3​ ​3 − 1  =  2​ ​2 − 1  =  1​ ​1 − 1  =  0​

b

WRITE

T

Value of i (updated) ​0 + 1  =  1​ ​1 + 1  =  1​ ​2 + 1  =  3​ ​1 − 1  =  0​

b apple banana cherry dragonfruit

D

R

a 4 3 2 1

Iteration Printed on screen 1 "apple" (item at index 0) 2 "banana" (item at index 1) 3 "cherry" (item at index 2) 4 "dragonfruit" (item at index 3) NO FURTHER ITERATIONS

AF

while condition ​0  ≤  3​ (TRUE) ​1  ≤  3​ (TRUE) ​2  ≤  3​ (TRUE ​3  ≤  3​ (TRUE) ​4  ≤  3​ (FALSE)

EXAMPLE 10B.2  Writing while loops a Write code containing a while loop that prints all numbers divisible by 5 from 100 to 0. b Write code that searches a list of numbers for the number 7 and prints the index of the 7 in the list. You may assume 7 is in the list. THINK

a 1 The numbers required are 100, 95, 90, 85, … ending at 0. 2 Initialise a variable (let’s call it n) at 100 and include n >= 0 as the condition in the while loop. 3 Inside the while loop, print the variable and then decrease the variable by 5.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 11

20-Apr-21 20:09:40


b 1 R ecall that the index of a list starts at 0. Initialise a variable (let’s call it i) at 0. 2 To keep searching the list as long as the current item is not 7, the while condition will be L[i] != 7. 3 Inside the while loop increase the index by 1 each time. 4 At the conclusion of the while loop print the current value of the index (i). WRITE

a n = 100 while n >= 0: print(n) n = n – 5

b i = 0 while L[i] != 7: i = i + 1 print(i)

Example 10B.3  Reading for loops

AF

a for number in [–7,5,2,–3,1]: if number > 0: print(number)

T

For each of the following for loops, write down what will be printed on the screen.

b L = ["apple", "banana", "cherry", "dragonfruit"] for i in range(3): print(L[i]) THINK

D

R

a 1 T he for loop will go through each of the numbers in the list in order, using each number as a dummy variable that stores each of the values one by one. 2 The first line inside the loop checks if the number is positive, and if it is, the number is printed. So all the positive numbers in the list will be printed, in the order that they appear. b 1 Recall that using for i in range(3) will make i take all integer values from 0 to 2 in order (the number itself is not included). 2 L[i] looks up the item at index i, and the items at indexes 0 to 2 are 'apple', 'banana' and 'cherry' respectively.

WRITE

a 5 2 1

b apple banana cherry

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 12

20-Apr-21 20:09:40


Example 10B.4  Writing for loops a Write code containing a for loop that prints each state in Australia, and underline the dummy variable in the for loop. b Write code containing a for loop that prints every integer from 0 to 999, and underline the dummy variable in the for loop. THINK

a 1 W rite a list containing each state in Australia. 2 Call the dummy variable “state” inside the for loop, and then print “state” inside the for loop. b 1 We can quickly generate every non-negative integer up to a certain value using for i in range(). Here we need range(1000) as we want to stop at 999. 2 The dummy variable is i and we need to print it inside the for loop. WRITE

AF

T

a for state in ["NSW", "QLD", "SA", "TAS", "VIC", "WA"]: print(state) b for i in range(1000): print(i)

Helpful hints

D

R

✔ Writing out a table for while loops may take time, but it is a very good way to visualise your thinking and allows you to proofread your work more easily. ✔ When working with while loops, it is common for students to accidentally end up with an infinite loop. This is a loop that repeats forever because its stopping condition is never reached. When this happens the code will continue to run until it is manually stopped or your computer has no more available memory. ✔ Remember the structure for while and for loops: for variable in list: command

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 13

20-Apr-21 20:09:40


EXERCISE 10B Introduction to loops 1–4

1–7

1 For each of the following while loops, write down what will be printed on the screen. You may find it useful to use a table to track each iteration. b a = 7 while a – 2 > 0: print(a–2) a = a – 1

c x = 1 while x*x*x < 1000: print(x*x*x) x = x+1

d x = 1 while x <= 10: print(2*x) x = x + 1

e a = 2 b = 5 while a+b > 0: print(a+b) a = a + 2 b = b – 4

f stations = ["Flinders", "Melbourne Central",

T

a n = 0 while n < 10: n = n+2 print(n)

"Parliament", "Southern Cross"]

i = 3

AF

10B.1

1–5

while i >= 0:

print(stations[i])

D

R

i = i – 1

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 14

20-Apr-21 20:09:41


10B.2

2 a Write code containing a while loop that will print the first 10 odd positive integers. b Write code containing a while loop that prints the first 5 square numbers (1, 4, 9, 16, 25). c Write code containing a while loop that will that will print each number ending in 5 between 0 and 50. 3 Write code that searches a list L for the number 3 and prints the index of 3 in the list when it finds it. You may assume 3 is in the list.

10B.3

4 For each of the following for loops, write down what will be printed on the screen: a for number in [–2, –4, –6, –8, –10]: print(number) b for number in [1,2,3,4]: print (3*number) c for number in [6, 4, 7, 3, 8, 2, 9, 1]: if number > 4: print(number) d treats = ["donut", "cake", "ice cream", "baklava"] for dessert in treats:

T

print(dessert)

e treats = ["donut", "cake", "ice cream", "baklava"] print(treats[i]) 10B.4

AF

for i in range(3):

5 a W rite code containing a for loop that prints each of the subjects that you do at school. Underline the dummy variable in the for loop. b Write code containing a for loop that prints every number from 0 to 365. Underline the dummy variable in the for loop.

R

6 Write code containing a for loop that searches a list of numbers called L for a negative number, and prints the index of the negative number when it finds it. Underline the dummy variable in the for loop. Hint: use len()to get the length of the list.

D

7 Explain why the following code produces an infinite loop (a loop that repeats forever). number = 0 while number < 10: print(number)

Check your student obook for these digital resources and more Interactive worksheet Loops

Investigation Loops in coding

Go further Year 9: 10A Nested loops

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 15

20-Apr-21 20:09:41


10C Application of loops Learning intentions ✔ I can justify whether I should use a while loop or a for loop to solve a problem

Inter-year links Year 7 11A Printing information to the computer screen

✔ I can write code containing loops to solve problems

Year 9

10A Nested loops

✔ I can debug code containing loops

Year 10

12A Two-dimensional lists

When to use loops in solving a problem Loops should generally be used whenever a problem requires a repeated procedure. Identifying this repeated procedure before starting to write code is very important. ➝ For example, consider finding the sum of the 20 numbers between 1 and 20. This can be done on a calculator, but it requires repeatedly adding the next number to the current total. This is a repeated procedure, and so can be done more efficiently using a loop.

T

Deciding between a for loop and a while loop

AF

Many problems can be solved equally well with a for loop or a while loop. If we know how many iterations (repetitions) we require, a for loop tends to be easier. For instance, in the above example a for loop will be easier because we know 19 presses of the + button on our calculator are required. If we want to continue the loop until a certain condition is met, and we don’t know how long that will take, using a while loop is advisable. For example, if you are trying to find the first square number bigger than 1000 you would not know ahead of time how many numbers to try, so using a while loop is a good idea.

R

• •

Debugging code involving loops Debugging code is the process of removing bugs (errors) from the code. Without debugging, your code may not run or may not work as you would like it to. To debug, go through the code line by line and track the progress of any variables and anything being printed.

D

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 16

20-Apr-21 20:09:41


EXAMPLE 10C.1  Evaluating numbers expressed as powers of positive integers Consider the problem of evaluating ​2​ 9​(recall ​2​ 9​  =  2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2​). a What is the repeated procedure you would need to use when evaluating powers of 2? b Is the number of repetitions required known? Does this suggest using a for loop or a while loop? c Write code including a loop that prints each power of 2 between ​2​ 1​to ​2​ 9​ THINK

WRITE

a To do this on a calculator, × 2 would need to be pressed repeatedly. b Starting at 2, the number needs to be multiplied by 2 eight times.

a The repeated process here is multiplying by 2.

c To loop through eight times use i in range(8). Initialise the variable as 2, print it, and then for each iteration multiply by 2 and print the updated variable.

c n = 2 print(n) for i in range(8): n = n * 2 print(n)

AF

T

b The number of repetitions required is known to be 8. This suggests using a for loop.

EXAMPLE 10C.2  Debugging code with simple errors

The following code was written by a student who was trying to print all multiples of 7 between 0 and 100. Unfortunately, it has several bugs (mistakes). List the bugs and debug the code. x = 0

2

While x is less than 100

R

1

x = x * 7

4

Print x

D

3

THINK

1 Line 1 defines a variable x that is equal to 0. 2 Line 2 has three errors: • Python commands (while, for, if, print etc) cannot be written with a capital letter. • The logic of the while loop is correct (is less than 100), but "is less than" is an English expression that cannot be used in Python. We need to use the < symbol instead. • Every loop or conditional statement must end with a colon (:) in Python. 3 Line 3 mistakenly multiplies the variable x by 7 when looking for multiples of 7. Multiples of 7 are 7 apart, so we should be adding 7 here. 4 Line 4 uses a capital letter for the print command, and is missing the brackets around the variable that is being printed.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 17

20-Apr-21 20:09:42


WRITE

Bugs: • Python commands such as while and print cannot be capitalised • ‘is less than’ is not recognised in Python (need to use <) • Missing colon on line 2. • Wrong operation on line 3. • Missing bracket on line 4.

x = 0 while x < 100: x = x + 7 print(x)

EXAMPLE 10C.3  Testing a number for divisibility

THINK

AF

T

Consider the problem of checking whether a given number is divisible by 7. A common strategy (as shown in Chapter 1) is to list all the multiples of 7 until we reach or exceed the given number. a What is the repeated procedure that this approach uses? b Is the number of repetitions required known? Does this suggest using a for loop or a while loop? c Write code including a loop that asks a user for an integer, and checks whether this integer is divisible by 7 by applying the described approach. WRITE

a The repeated process is adding 7 to the running total. b The number of repetitions is not known ahead of time, which suggests using a while loop.

D

R

a Listing multiples of 7 describes repeatedly adding 7 to the total. b The number of multiples that are needed in the list isn’t know, so the number of repetitions required is not known. c 1 A running total can be represented by a variable called total that is initialised at 0. 2 Since we want to keep adding 7 until the number is reached or exceeded, this should be the condition of the while loop. 3 Inside the loop, the repeated procedure is done (adding 7 to the total). 4 After the loop stops, an if statement can be used to check whether the total reached the number (divisible by 7) or exceeded it (not divisible by 7).

c number = int(input("Pick a number: ")) total = 0 while total < number: total = total + 7 if total == number: print("divisible by 7") else: print("not divisible by 7")

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 18

20-Apr-21 20:09:42


EXAMPLE 10C.4  Testing whether a list is sorted Consider the problem of checking whether a list of numbers is sorted. For example, the list [1, 4, 5, 7, 9, 11, 15, 22] is sorted, while the list [2 ,5, 4, 9, 18, 20, 15, 23] is not sorted, since 4 comes after 5 and 15 comes after 20. a What is the repeated procedure you need to use when checking whether a list is sorted? b Is the number of repetitions required known? Does this suggest using a for loop or a while loop? c Write code including a loop that checks whether a list is sorted. You will need to define a list variable in the first line of the code. WRITE

a The repeated process is comparing an item to the following item. b The number of comparisons is known ahead of time, which suggests using a for loop c L = [2,5,4,9,18,20,15,23] is_list_sorted = "sorted" for i in range(len(L)–1): if L[i+1] < L[i]: is_list_sorted = "not sorted" print(is_list_sorted)

D

R

AF

a Checking whether a list is sorted requires comparing each pair of neighbouring items. b The number of items in a list is known, therefore the number of comparisons required is also known. c 1 Use a variable (is_list_sorted) and initially assume the list is sorted. 2 Use len(L) – 1 as the limit in a for i in range(): loop. 3 Inside the loop use an if statement to compare each item (L[i]) to the item following it (L[i+1]). If any pair is out of order change the variable to "not sorted". 4 Print the variable with the result.

T

THINK

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 19

20-Apr-21 20:09:42


Example 10C.5  Debugging a search procedure Search algorithms search for a specific item in a list (or other types of data). The following three search algorithms all attempt to search for the highest value in a list. Each of the algorithms contains at least one error. Describe the error in each algorithm and debug the algorithm (fix the error).

b L = [2,7,9,5,3,4] highest = 0 i = 0 while L[i] > highest: highest = L[i] i = i + 1 print(highest)

R

THINK

AF

c L = [2,7,9,5,3,4] highest = 0 i = 0 for i in range(len(L)): if i > highest: highest = i print(highest)

T

a L = [2,7,9,5,3,4] highest = 0 for number in L: if number > highest: number = highest print(highest)

D

a 1 C heck that the list and the variables have been correctly initialised. The list has been initialised as L, and highest is initialised as the variable that tracks the highest value seen in the list. 2 Look at the code within the for loop. The for loop goes through each number in L, using number as the dummy variable. The comparison in the if statement is correct; however, the line inside the if statement attempts to change the dummy variable (number), rather than changing the value of highest. b 1 Check that the list and the variables have been correctly initialised. The list has been initialised as L, highest is initialised as the variable that tracks the highest value seen in the list, and i is initialised as a dummy variable. 2 Look at the code within the while loop. The condition in the while loop seems reasonable, however it will terminate as soon as the current item is less than the value of highest. In the case of the specific list given it would work, however it would not work if the last element in the list were changed to a higher number than 9. c 1 Check that the list and the variables have been correctly initialised. The list has been initialised as L, highest is initialised as the variable that tracks the highest value seen in the list, and i is initialised as a dummy variable. 2 Look at the code within the for loop. The loop goes through the integers corresponding to the index of the items in the list, and compares these to the value of highest. Instead of comparing the values of the index in the list, we should be comparing the item at that index. The same applies to the following line when assigning a new value to highest.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 20

20-Apr-21 20:09:42


WRITE

a There is an error in line 5 of the code. Rather than assigning the value of highest to number, the value of number should be assigned to highest. Line 5 should instead be: highest = number b The condition in the while loop is incorrect. The while loop should continue until the end of the list is reached, and there needs to be an if statement inside the while loop checking whether the current item is larger than highest. The corrected code is:

T

L = [2,7,9,5,3,4] highest = 0 i = 0 while i < len(L): if L[i] > highest: highest = L[i] i = i + 1 print(highest)

AF

c Lines 4 and 5 should refer to the item at index i, but instead refer to the value of the index. To debug the code replace i with L[i] in both lines.

Helpful hints

D

R

✔ Be patient! In this exercise you are writing code involving loops to solve problems that you have seen (or will see) in your Mathematics classes. This is a complex skill and requires practice and care. Making mistakes along the way is an important part of learning. ✔ Most of the worked examples and questions in this exercise focus on a specific number, for example, checking if a number is divisible by 7. However, the power of code means that once you have written code that works for one number, it is often very easy to tweak the code to solve a similar problem involving any number.

EXERCISE 10C Application of loops 1, 5–7

10C.1

1–7

1–11

1 Consider the problem of evaluating ​5​ 7​(recall ​5​ 7​  =  5 × 5 × 5 × 5 × 5 × 5 × 5​) a What is the repeated procedure you would need to use when evaluating powers of 5? b Is the number of repetitions required known? Does this suggest using a for loop or a while loop? c Write code including a loop that prints each power of 5 between ​5​ 1​to ​5​ 7​​.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 21

20-Apr-21 20:09:42


2 The factorial of a positive integer is the product of every integer between 1 and that integer. For example, 5 factorial (written as 5!) is equal to 5 ​  × 4 × 3 × 2 × 1​. Consider the problem of evaluating 20! (20 factorial). a What is the repeated procedure you would need to use in trying to evaluate 20 factorial? b Is the number of repetitions known ahead of time? Does this suggest you should use a for loop or a while loop? c Write code including a loop that prints the factorial of each number between 1! and 20! 3 Over the past decade, the population of Melbourne has increased by approximately 2% each year. The current population of Melbourne is approximately 5,000,000. a Estimate what the population of Melbourne will be in one and two years’ time? b What is the repeated procedure required to work out the population of Melbourne over the next 30 years? c Write code that will print Melbourne’s population for each of the next thirty years.

AF

T

4 Inflation in economics describes the percentage increase in the average price of goods across a year. An inflation of 3% suggests that, on average, goods are 3% more expensive than in the previous year. If the current price of a block of chocolate is $4, we would like to estimate the price of a block of chocolate for each of the next 10 years, assuming the inflation rate remains at 3%. a Estimate how much the block of chocolate will cost in one and two years’ time? b What is the repeated procedure required to work out the price of the block of chocolate for each of the next 10 years? c Write code to calculate the price of the block of chocolate for each of the next 10 years.

R

5 The following code was written by a student who was trying to print all even numbers between 20 and 30 inclusive. Unfortunately, it has several bugs (mistakes). List the bugs and debug the code. 1

x == 20

2

while x < 30

3

x + 2

4

D

10C.2

print(x)

6 The following code was written by a student who was trying to print the first 10 square numbers. Unfortunately, it has several bugs (mistakes). List the bugs and debug the code. 1

10C.3

while x < 10:

2

print(x*2)

3

x + 1

7 Consider the problem of checking whether a given number is divisible by 11, without using division. a What is a repeated procedure you can use to check whether a number is divisible by 11? b Is the number of repetitions required known? Does this suggest using a for loop or a while loop? c Write code including a loop that asks a user for an integer, and checks whether this integer is divisible by 11. Do not use division within your code.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 22

20-Apr-21 20:09:44


8 In Python, the % (modulus) operator can also be used to check for divisibility. Writing X % Y in Python is equivalent to saying, “What is the remainder when you divide X by Y?”. Therefore, if X is divisible by Y, X % Y = 0. Consider the problem of finding every factor of a 3-digit integer by trying to divide it by every integer from 1 to itself. Using the % symbol as described above, write code involving a loop that prints every factor of a given 3-digit number. 10C.4

9 a Write code including a loop that checks whether a list is sorted in descending order. b Write code including a loop that checks whether every number in a list is the same. 10 Write code including a loop that searches for the minimum value in a list of integers.

10C.5

11 A Year 8 student, Karina, has a list of the first names of every student at her school. She would like to know whether there are any other Karinas at the school. She knows that one of the Karinas at the school will be her, so she is interested in searching for a second Karina in the list. Consider the problem of finding whether there is more than one "Karina" in a given list. The following three algorithms are Karina’s attempts at solving this problem. Debug each algorithm and amend the errors to fix the algorithms.

AF

T

a for name in L: if name == "Karina": number_of_karinas = number_of_karinas + 1 if number_of_karinas > 1: print("I am not the only Karina!") b number_of_karinas = 0 i = 0 while i < len(L): if L[i] == "Karina": number_of_karinas = number_of_karinas + 1 print("I am not the only Karina!")

D

R

c number_of_karinas = 0 for i in range(len(L)): if i == "Karina": number_of_karinas = number_of_karinas + 1 if number_of_karinas > 1: print("I am not the only Karina!") Check your student obook for these digital resources and more Interactive skillsheet Applications of loops

Investigation Loop-di-loop

Go further Year 9: Nested loops

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means. 10_OM_VIC_Y8_SB_29235_TXT_3PP.indd 23

20-Apr-21 20:09:44


Given a fixed perimeter, the shape with the largest possible area you can make is a circle! Similarly, given a fixed surface area, the shape with the largest possible volume is a sphere – like bubbles!

ISBN 978-0-19-032923-5

9 780190 329235 visit us at: oup.com.au or contact customer support: oup.com.au/help


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.