Question 1
1 markA program must ask the user for their age and store it as a whole number in the variable age.
Which line does this correctly?
A age = input(int("Enter your age"))
B age = int(input("Enter your age"))
C int(age) = input("Enter your age")
D input(age) = "Enter your age"Choose your answer.
- A
- B
- C
- D
Model answer
B: age = int(input("Enter your age"))
Explanation
input() runs first, displaying the prompt and returning what the user types as a string. int() then converts that string to an integer, and the assignment stores it in age. The variable being assigned must be on its own on the left of =, which rules out C and D, and in A int() is wrongly applied to the prompt instead of to the user's input.
Mark scheme
-
1 mark
B / age = int(input("Enter your age"))
Accept the letter B or the line itself. A tries to convert the prompt text to an integer; C and D have a function call on the left of the assignment, which is not allowed. If more than one option is given, 0 marks.