GCSE Programming Questions

The attendance officer at OCR school has to analyse lots of register data on a daily basis.

Registers are stored in the following format:

register = ["P","P","N","P","P"]

1a) Write a program to create a register like the one above. You should complete the following steps:

-Ask the user for a five seperate marks

-These should be added to an array

-The array should be output at the end

[5 marks]

Show Answer
1 register = []
2 for x in range(5):
3     print("Enter a mark")
4     m = input()
5     register.append(m)
6 print(register)

1b) Assume register is already defined and could contain any combination of marks. Print out how many "P" marks and how many "N" marks are in the array register.

[7 marks]

Show Answer
1 n = 0
2 p = 0
3 for x in range(len(register)):
4     if register[x] == "N":
5         n = n + 1
6     else:
7         p = p + 1
8 print("P:", p)
9 print("N:", n)

The amount of valid marks has expanded to include more than "N" and "P" and contains 6 different valid characters. The function isValidMark() takes 1 parameter and returns the string "valid" if it is valid and "invalid" if it is not.

1c) Take an input from the user and using the function, print "invalid" or "valid" if the character entered is valid.

Assume the function mentioned above is already defined and you do not have to create it yourself.

[4 marks]

Show Answer
1 print("Enter a mark")
2 mark = input()
3 v = isValidMark(mark)
4 print(v)

You are a software developer working for a company that sells pet supplies online. The company has recently decided to add a feature to their website that displays the top 5 most popular pet names.

The array of most popular pet names isn't always of length 5 and can be bigger or smaller.

1a) Create a procedure called topFive() that takes one parameter. The parameter could be an array of any size.

-If the array has more than 5 names, you should only print out the first five

-If the array has five names or fewer, all names should be printed out

[5 marks]

Show Answer
1 def topFive(arr):
2     for x in range(len(arr)):
3         if x == 5:
4             break
5         print(arr[x])

The name array has been provided as the following:

petNames = ["Bella","Max","Charlie","Lucy","Daisy","Milo","Oliver"]

1b) Use your function above to print out the top 5 pet names. You can assume the array is already defined

[2 marks]

Show Answer
1 topFive(petNames)

The company has decided to read the names from a file instead. The file is called "petNames.txt". The file contains exactly 5 names.

1c) Write a program to print out all the names to the screen

[7 marks]

Show Answer
1 f = open("petNames.txt","r")
2 lines = f.read().split("\n")
3 f.close()
4 for x in range(len(lines)):
5     print(lines[x])

A supermarket is closing early for Christmas and has a sale on some items in the shop. All the items on sale have a 50% discount.

1a) Write a function called newPrice that has two parameters, the first is the normal price of the item and the second is the string "y" or "n". "y" represents that the item is on sale and "n" means it is not. The function should return the new price if it is on sale or the original price if not.

[4 marks]

Show Answer
1 def newPrice(original,sale):
2     if sale == "y":
3         return original * 0.5
4     else:
5         return original

At the supermarket tills many items are scanned and a final price is given.

1b)Write a program that completes the following:

-Asks for the price of the item

-Asks if the item is on sale

-Uses the function created above to work out the new price of the item

-Repeats steps 1,2 and 3 until a price of 0 is entered

-Outputs the total cose at the end

[8 marks]

Show Answer
1 total = 0
2 while True:
3     print("Enter a price")
4     price = input()
5     if price == 0:
6         break
7     print("Is the item on sale?")
8     sale = input()
9     c = newPrice(price,sale)
10     total = total + c
11 print(total)

A games company is designing a tower defence game. Each monster is stored using an array:

arr = ["mon1",56]

The first index stores the player's name and the second stores its health. Throughout the game the health changes and the above is only an example.

1a) Assuming the array is already defined but could be any value, write code to check if the monster is alive or dead. Your program should output "Alive" if the health is above 0 and dead if it is not.

[3 marks]

Show Answer
1 if arr[1] > 0:
2     print("Alive")
3 else:
4     print("Dead")

To help debug their code the developers write a function to output the monsters name and their current health in the following format:

mon1: 56

1b) Write a function called printMonster that takes one parameter, the array mentioned above, and outputs the name and health in the format above

[3 marks]

Show Answer
1 def printMonster(a):
2     print(a[0] + ":" + a[1])

The developers are ready to test their game and want to create a feedback program. The program should do the following:

- Ask for a a rating between 1 and 5

- Repeat step 1 until "stop" is entered

- Output the total amount of ratings

- Output the average of all ratings input

1c) Create a program that matches the criteria above, you do not need to validate the input

[7 marks]

Show Answer
1 ratingTot = 0
2 amount = 0
3 while True:
4     print("Enter a rating")
5     i = input()
6     if i == "stop":
7         break
8     amount = amount + 1
9     ratingTot = ratingTot + i
10 print(amount)
11 print(ratingTot/amount)