Year 10TechnologySupport
Read each program carefully and run it in your head, line by line. Indented lines belong to the line above that ends with a colon. Use a trace table on paper to keep track of each variable's value. When you write code, use correct Python with colons and indentation.
AReading Python code
- 1.
What does this program print? score = 7 score = score + 3 print(score)
(1 mark)- 10
- 7
- 73
- score + 3
- 2.
Word bank: for, while, if A loop repeats a set number of times, such as once for each item in a list. A loop keeps repeating as long as its condition is True.
(2 marks) - 3.
Worked example: range(3) gives the numbers 0, 1, 2. It starts at 0 and stops before 3. Now write down every number this program prints. for n in range(5): print(n)
(1 mark) - 4.
What does this program print? temp = 18 if temp > 20: print("Shorts") else: print("Jacket")
(1 mark)- Jacket
- Shorts
- Shorts and Jacket
- Nothing
- 5.
Python counts the items in a list from 0, so the first item is towns[0]. towns = ["Whangārei", "Tauranga", "Nelson", "Dunedin"] print(towns[2]) What is printed?
(1 mark)
BTracing and fixing
- 6.
Trace this program by filling in the total after each time through the loop. Then write what is printed. total = 0 for coins in [2, 5, 1]: total = total + coins print(total)
(2 marks)Trace table
coins total (start) 0 2 ? 5 ? 1 ? - 7.
This program crashes with a NameError. name = input("What is your name? ") print("Hello " + Name) What is wrong, and how do you fix it? Sentence starter: The error happens because ...
(2 marks) - 8.
In New Zealand you can sit your learner licence at 16. Finish this program so it prints "You can sit your learner licence" if the age is 16 or more, and "Not yet" if it isn't. The first line is done for you: age = int(input("How old are you? "))
(3 marks)