Professional Documents
Culture Documents
Questions
Questions
These exercises will cover all the python syntax we need to know for paper 4 of the syllabus A
level computer science syllabus 9618.
Important notes:
● These exercises only cover the syntax. They do not cover fundamental and abstract
concepts like sorting algorithms, abstract data types etc….
● The checklist on which these exercises are based is complete to the best of my
knowledge, but I cannot guarantee that I have not missed something. Let me know in
the comments if you think I skipped something that could come up on an exam.
The Basics:
Get a number from the user between 10 and 100.
Cast the input received from the user to make sure it is an integer.
Print an error message in case the user did not input a number, and ask the user to try
again
Print an error message in case the user input a number outside of the given range (use
if … elif … else …)
Print an error message in case the user input a number outside of the given range (use
logical operators)
Start with a value of 1 and double it N times, where N is the number given by the user
(use a for-loop). Each iteration print “We have doubled X times resulting in Y”.
Find out how many times you can double the value 1 until it exceeds 1,000,000.
Get the user’s favourite colour. Using match/case: if the colour is red output ‘You are a
passionate person’, if the colour is blue output ‘You are a reliable person’. In all other
cases output ‘I don’t know that colour’
Arrays:
Declare an array with the values ‘a’, ‘b’, ‘c’, 1, 2, ‘z’
1/4
Name Biology grade English grade
John 9 6
Jane 7 5
Michael 4 7
Mary 8 7
Declare a 2D array with 50 rows and 10 columns. Initialise the array so that each
element has a value of None.
Functions:
Write a function that asks the user to enter their name and then print “Hello “ + name.
Write a function that takes 3 string values as arguments, determines the number of
letters in each string, calculates the average of the three lengths, and returns the
resulting value.
Modify the above function so that it also calculates the sum and returns both the sum
and the average.
2/4
Declare an array with length 10 and width 3.
Read each line of student_grades.txt and populate the first column of the array with the
student’s names, populate the second column with the corresponding Biology grade and
populate the third column with the corresponding English grade. Make sure you read
until the end of the file.
Create a new file: ‘whole_class_summary.txt’ and write the following line to the file. Then
close the file again.
Summary for class 6A
Open the file for append and, line-by-line, add each student’s name followed by their
average grade. Then close the file again.
Classes
Create a class ‘Caveman’. The class has the following private properties:
○ hungry (boolean)
○ name (string)
The constructor takes a string as a parameter which sets the name of the Caveman. The
property hungry is set to True by default when a new instance of ‘Caveman’ is created.
We also output a message “[name] was born”.
The class has one public method named ‘eat’. In this method we set the value of hungry
to False and we output “Gobble gobble gobble”.
The class also has a destructor. In the destructor we output “Oh no! A sabre tooth tiger
ate [name]!”.
Sadly ‘Glug’ one day met his demise. Delete the instance of ‘Glug’
Create a new class named ‘ModernMan’. The class inherits all the properties and
methods of ‘Caveman’
3/4
○ bankBalance (integer) which has an initial value of 0
○ showBalance() which prints out the current balance.
ModernMan also eats in a much more polite way, unless he is hungry. Override the eat
method so then he outputs “chew chew” in case he is not hungry. But if he is hungry then
run the original method from the super class.
Before ModernMan dies he’s not so worried about sabre tooth tigers but is more
concerned with updating his last will and testament. Override the destructor so that he
outputs “[name] updates his/her last will and testament” instead.
Add a method named ‘earnMoney’. The method can be called with no parameters in
which case 10 is added to the bank balance. Optionally, we can specify the amount we
want to add to the balance as one of the parameters. Implement this using overriding.
Create a new file and add Mark to it. Then close the file.
Append the file with George, Amy and Clara. Then close the file.
Read the third record from the file and print the id. Then close the file.
Have the user input a student name. Read the file and search for a student with the
name. Output the student_id of the student or "not found"
Insert the student Anne into the file so that the students are arranged in order of student
id.
4/4