In Figure 5, Python collects five whole numbers from the keyboard, converting each with int(), and displays the sum.
Define the term data type.
Our own questions on this topic, marked straight away, so you see what to revise. Free now.
Show model answer for 05.1Hide model answer for 05.1
A data type says what kind of data a variable holds, such as a whole number, a real number or text. It also decides which operations can be used on that value.
How the marks are earned
- A data type specifies the kind of value or data a variable holds.
- Alternatively, it fixes the operations that may be performed on whatever a variable holds.
- Writing information in place of data does not earn the mark.
Explain why num1 to num5 are not given a string data type.
See how you would score on Data TypesShow model answer for 05.2Hide model answer for 05.2
Strings would be joined, not added. For example "4" + "7" would give "47" instead of 11, so total would be wrong.
How the marks are earned
- With string variables, + would stick the numbers together as text (concatenation) rather than adding them.
- Or the numbers have to be added to each other.
- Or + works as arithmetic only when the values on both sides of it are numbers.
Name the Python data type that holds numbers with a fractional part.
See how you would score on Data TypesShow model answer for 05.3Hide model answer for 05.3
float. A float holds a number with a fractional part, such as 3.5.
Rewrite Figure 5 so a loop with only one input() call totals the five numbers. Use meaningful variable names.
See how you would score on Iteration (Loops)Show model answer for 05.4Hide model answer for 05.4
sum_so_far = 0
for turn in range(1, 6):
amount = int(input())
sum_so_far = sum_so_far + amount
print(sum_so_far)How the marks are earned
- An iteration structure that exists in the language is used (design mark).
- The loop condition makes it repeat exactly five times.
- User input is used once, in a sensible place inside the loop.
- The total of the entered values is built up correctly.
- The total is output once, in a sensible place after the loop.
- The case of the code is ignored; messages with input and output are optional; gaps are ignored unless they change the logic.
- If the code has any error, the maximum is 4 marks.
- The design mark is for choosing a suitable technique and is credited whether or not the syntax is correct and whether or not the program works.
Why it scores
The for loop is the iteration, and range(1, 6) makes it run exactly five times. The one input() call sits inside the loop, so a new number is read on every pass. Each number is added to sum_so_far, which starts at 0. The print is outside the loop, so the total shows only once, at the end.
Revise Iteration (Loops)