Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Solve the following problems in python:

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’

Loop through the array and print each value.

Declare a 2D array with the values below:

1/4
Name Biology grade English grade

John 9 6

Jane 7 5

Michael 4 7

Mary 8 7

Use a loop and an if to look up Michael’s English grade.

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.

The following questions explore the concepts of passing parameters to functions by


value, by reference and by making a variable global.
Declare three variables: a = “pass_by_value”, b = “pass_by_reference”, c =
“global” , d = “not_global” and e = [“mutable1”,”mutable2”]. Print each variable.
Write a function that takes ‘a’ as an argument passed by value, ‘b’ as an
argument passed by reference and defines ‘c’ as global.
The function should convert variables ‘a’, ‘b’ and ‘c’ to uppercase. Within the
function print each variable just after they have been converted to uppercase.
After calling the function print each variable. For which variables did the changes
persist?
Modify the function so that the variable ‘d’ is converted to upper case letters. (Do
not pass the variable as a parameter and do not make it global.) Is this possible?
Modify the function so that the first element of variable ‘e’ is also converted to
upper case. (LDo not pass it as a parameter and do not make it global.) Is this
possible? Does the change persist?

Handling Text files


Open the file ‘student_grades.txt’ for reading.

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.

Close 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 has two getters:


○ isHungry() returns the value of __hungry
○ getName() returns the name of the caveman

The class also has a destructor. In the destructor we output “Oh no! A sabre tooth tiger
ate [name]!”.

Create a new instance of Caveman, a caveman named ‘Glug’.

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’

But he gets an additional private property and an additional method:

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.

Handling Binary Files


Create a class named Student with the following properties:
○ Name (string)
○ StudentID (int)

Create an array of 5 students with the following values:


○ Mark, 1
○ George, 3
○ Amy, 4
○ Clara, 9
○ Anne, 2

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

You might also like