Key Stage 3 · Y7.3
Introduction to Python
Name
Class
Date
Mark / 25
Section A
Circle the letter of the correct answer. One mark each.
- 1What does
print("7" + "3")display?1- A7 3
- B73
- CAn error
- D10
- 2What is stored in
ageafterage = input("Age? ")if the user types 14?1- ATrue
- BThe number 14
- CNothing
- DThe text "14"
- 3Which line correctly converts input into a whole number?1
- Aage = input("Age? ").number
- Bage = str(input("Age? "))
- Cage = int(input("Age? "))
- Dage = input(int("Age? "))
- 4What is wrong with
if score = 10:?1- Ascore should be in quotes
- BNothing is wrong
- CThere should be no colon
- DA comparison needs two equals signs
- 5What does
elifmean?1- Aeither if
- Belement if
- Celse if
- Dend if
- 6A program uses
if mark >= 30beforeif mark >= 70. What happens with a mark of 85?1- AThe first condition is true, so the wrong message is shown
- BThe program crashes
- CThe correct message is shown
- DBoth messages are shown
- 7Which data type is
3.5?1- Aint
- Bstr
- Cfloat
- Dbool
- 8What causes a NameError?1
- AUsing a variable that has not been created, or a typo in its name
- BAdding a string to a number
- CForgetting a colon
- DBad indentation
- 9Why must lines inside an if statement be indented?1
- APython ignores indentation
- BIndentation tells Python which lines belong inside the if
- CIt makes the program run faster
- DIt makes the code look nicer
- 10What does
str(42)produce?1- ATrue
- BThe number 42
- CAn error
- DThe text "42"
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1ExplainExplain why the following code causes an error: age = input("Age? ") then print(age + 1)3
- 2WriteWrite a Python program that asks the user for their name and their age, then prints a message saying whether they are old enough to watch a 12 rated film.5
- 3StateState the purpose of the int() function and the str() function in Python.2
- 4ExplainA program should print 'Pass' for a mark of 50 or more and 'Fail' otherwise, but a mark of exactly 50 prints 'Fail'. Explain the likely cause and the correction.3
- 5ExplainExplain why indentation matters in Python.2
Mark scheme
Section A
Section B
1 Explain why the following code causes an error: age = input("Age? ") then print(age + 1) 3
One mark for each of the following, to a maximum of 3.
- The input function returns a string
- Python cannot add a number to a string
- The fix is to convert the input using int()
Model answer. The input function always returns a string, so age holds the text "14" rather than the number 14 even when the user types digits. Python cannot add the integer 1 to a string, so it raises a TypeError. The fix is to convert the input to a whole number as it is read, by writing age = int(input("Age? ")), after which the addition works correctly.
2 Write a Python program that asks the user for their name and their age, then prints a message saying whether they are old enough to watch a 12 rated film. 5
One mark for each of the following, to a maximum of 5.
- Asks for the name using input
- Asks for the age and converts it to an integer
- Uses an if statement with a correct condition
- Prints a suitable message when old enough
- Uses else to print a message when not old enough
Model answer. name = input("What is your name? ") age = int(input("How old are you? ")) if age >= 12: print(name + ", you can watch a 12 rated film.") else: print(name + ", you are too young for a 12 rated film.") The name is read as a string, which is correct because it is text. The age is converted to an integer so it can be compared with a number. The condition uses greater than or equal to, so someone who is exactly 12 is correctly allowed.
3 State the purpose of the int() function and the str() function in Python. 2
One mark for each of the following, to a maximum of 2.
- int() converts a value into a whole number
- str() converts a value into text or a string
Model answer. The int() function converts a value into a whole number, which is needed when input has been read from the user as text but arithmetic or a numerical comparison has to be performed on it. The str() function does the opposite, converting a value such as a number into a string, which is needed when you want to join it to other text using the plus operator.
4 A program should print 'Pass' for a mark of 50 or more and 'Fail' otherwise, but a mark of exactly 50 prints 'Fail'. Explain the likely cause and the correction. 3
One mark for each of the following, to a maximum of 3.
- The condition uses greater than rather than greater than or equal to
- A mark of exactly 50 is not greater than 50, so it fails the test
- Change the condition to mark >= 50
Model answer. The likely cause is that the condition has been written as if mark > 50 rather than if mark >= 50. A mark of exactly 50 is not greater than 50, so the condition is false and the else branch runs, printing Fail. The correction is to change the operator to greater than or equal to, writing if mark >= 50, so that a mark of exactly 50 is correctly counted as a pass.
5 Explain why indentation matters in Python. 2
One mark for each of the following, to a maximum of 2.
- Indentation shows which lines belong inside a structure such as an if statement or a loop
- Incorrect indentation causes an error or makes the program behave incorrectly
Model answer. In Python, indentation is not just formatting: it is how the language knows which lines belong inside an if statement, a loop or a function. Lines indented under an if only run when the condition is true, while a line that is not indented runs regardless. If the indentation is inconsistent Python raises an IndentationError and the program will not run at all, and if it is consistent but wrong the program will run while doing something the programmer did not intend.