- May 23, 2023
Mastering Cambridge 9618 Paper 4 Practical: Past Paper Solutions Part 2
- Haguin School
- 0 comments
YouTube Video
What You Will Need
2022 October/November - 9618/42 Question Paper
2022 October/November - 9618/42 Mark Scheme
2022 October/November - 9618/42 evidence.doc
2022 October/November - 9618/42 Characters.txt
Free download using this link
Q2
Define Character Class
Write __init()__ method with self, Name, XCoordinate, YCoordinate as input arguments
Since it's private, we need to use "__" in the declaration
Write comment for attribute declaration e.g. private (variable) as (datatype)
Typical class question, just have to be careful on double underscore (__) for private attributes, and you canβt access private attributes outside the class.
class Character:
# private Name as string
# private XCoordinate as integer
# private YCoordinate as integer
def __init__(self, Name, XCoordinate, YCoordinate):
self.__Name = Name
self.__XCoordinate = XCoordinate
self.__YCoordinate = YCoordinateWrite methods for GetName return name
Write methods for GetX() return x coordinate
Write methods for GetY() return y coordinate
This is free marks really π The only thing that you have to be careful is youβre supposed to put the methods within the class Character. The answer scheme is a bit misleading though, you might think that you have to define the methods outside the class but all these methods are supposed to be part of the class Character definition!
def GetName(self):
return self.__Name
def GetX(self):
return self.__XCoordinate
def GetY(self):
return self.__YCoordinateWrite method for ChangePosition()
XCoordinate = XCoordinate + XChange
YCoordinate = YCoordinate + YChange
The question description has told us what to do for this method, so it should be a breeze as well π Remember that this method is part of the class definition
def ChangePosition(self, XChange, YChange):
self.__XCoordinate = self.__XCoordinate + XChange
self.__YCoordinate = self.__YCoordinate + YChangeAfter doing (a), (b), (c). The full class Character definition should be shown as below with all the private attributes and methods.
class Character:
# private Name as string
# private XCoordinate as integer
# private YCoordinate as integer
def __init__(self, Name, XCoordinate, YCoordinate):
self.__Name = Name
self.__XCoordinate = XCoordinate
self.__YCoordinate = YCoordinate
def GetName(self):
return self.__Name
def GetX(self):
return self.__XCoordinate
def GetY(self):
return self.__YCoordinate
def ChangePosition(self, XChange, YChange):
self.__XCoordinate = self.__XCoordinate + XChange
self.__YCoordinate = self.__YCoordinate + YChangeDeclare an empty array and name of text file
Open the ποΈ in READ mode and read line by line (keywords: try, except, open, readline, close)
Use the strip() function to remove all whitespacecharacters for good practice
-
Inside the file, run πββοΈ the loop for 10 times since 10 characters
Each loop will read 3 lines in sequence
Store each line into separate name, int(xcoord), int(ycoord) and then store it into TempCharacter object
Store Character object into 1d array
As long as you are able to visualise the workflow of this question, the solution is relatively straightforward. Half of the battle is won if you can visualise the final solution!
Characters = []
TextFile = "Characters.txt"
try:
File = open(TextFile, 'r')
for x in range(0,10):
Name = File.readline().strip()
XCoordinate = File.readline().strip()
YCoordinate = File.readline().strip()
TempCharacter = Character(Name,int(XCoordinate),int(YCoordinate))
Characters.append(TempCharacter)
File.close()
except:
print("File not found")-
Run a while loop based on condition of Position, set initial value of Position to -1
If matching Name is found, Position will get updated from -1 to either 0-9, exit the loop
-
Within the loop
Takes in input using method input()
Loop through 10 characters inside the array to find matching Character's Name
Since Name is a private attribute, we would need to use GetName() to get the private attribute Name
During comparison, convert both input and character's name from text file to lowercase
Only stop when the name is found, update Position = current index to exit while loop
Position = -1
while(Position == -1):
InputName = input("Please enter name to get position")
for Count in range(0,10):
TempName = Characters[Count].GetName()
if TempName.lower() == InputName.lower():
Position = CountUse the Position variable from (g)
-
Run a while loop based on boolean IsValid with initial value of False
Within while loop, take in the input (can write something like which character is for which direction)
And check if it's 'A'/'W'/'S'/'D' using a if statement, make sure uppercase the input just in case input is small letter
If matches, Update the position accordingly using ChangePosition method
Finally, update IsValid to True to end the while loop
Fun Fact π : this is actually based on the keyboard π» layout, look at your keyboard and you'll understand π‘
IsValid = False
while(IsValid==False):
Direction = input("A for left, W for up, S for down, D for right")
if (Direction.upper()=='A'):
Characters[Position].ChangePosition(-1,0)
IsValid = True
elif (Direction.upper()=='W'):
Characters[Position].ChangePosition(0,1)
IsValid = True
elif (Direction.upper()=='S'):
Characters[Position].ChangePosition(0,-1)
IsValid = True
elif (Direction.upper()=='D'):
Characters[Position].ChangePosition(0,1)
IsValid = Trueprint Name using GetName()
print XCoordinate using GetX(), conver to str
print YCoordinate using GetY(), convert to str
If you know Python string manipulation, this is a piece of π°.
print(Characters[Position].GetName()," has changed coordinates to X = ",str(Characters[Position].GetX())," and Y = ",str(Characters[Position].GetY()))Run everything from 2(a) to 2(g)(i)
Pass in above inputs
By right, if you have done 2(a) to 2(g)(i) correctly, this should work! βοΈ
# Run everything from 2(a) to 2(g)(i)
# Pass in above inputs
class Character:
# private Name as string
# private XCoordinate as integer
# private YCoordinate as integer
def __init__(self, Name, XCoordinate, YCoordinate):
self.__Name = Name
self.__XCoordinate = XCoordinate
self.__YCoordinate = YCoordinate
def GetName(self):
return self.__Name
def GetX(self):
return self.__XCoordinate
def GetY(self):
return self.__YCoordinate
def ChangePosition(self, XChange, YChange):
self.__XCoordinate = self.__XCoordinate + XChange
self.__YCoordinate = self.__YCoordinate + YChange
Characters = []
TextFile = "Characters.txt"
try:
File = open(TextFile, 'r')
for x in range(0,10):
Name = File.readline().strip()
XCoordinate = File.readline().strip()
YCoordinate = File.readline().strip()
TempCharacter = Character(Name,int(XCoordinate),int(YCoordinate))
Characters.append(TempCharacter)
File.close()
except:
print("File not found")
Position = -1
while(Position == -1):
InputName = input("Please enter name to get position")
for Count in range(0,10):
TempName = Characters[Count].GetName()
if TempName.lower() == InputName.lower():
Position = Count
IsValid = False
while(IsValid==False):
Direction = input("A for left, W for up, S for down, D for right")
if (Direction.upper()=='A'):
Characters[Position].ChangePosition(-1,0)
IsValid = True
elif (Direction.upper()=='W'):
Characters[Position].ChangePosition(0,1)
IsValid = True
elif (Direction.upper()=='S'):
Characters[Position].ChangePosition(0,-1)
IsValid = True
elif (Direction.upper()=='D'):
Characters[Position].ChangePosition(0,1)
IsValid = True
print(Characters[Position].GetName()," has changed coordinates to X = ",str(Characters[Position].GetX())," and Y = ",str(Characters[Position].GetY()))Hope you enjoy this walkthrough, comment below what you think! π