The assessment is a
question-based task.
In a
computer game, a bonus score is awarded to a player who guesses the location of
an enemy target area. The enemy target areas, for example H2 and G4, are stored
in an array called areas.
Read the
following code carefully. It is not a complete programme.
Line |
|
1 |
def loadTargetAreas(): |
2 |
counter = 0 |
3 |
fileName = open("board.csv",
"r") |
4 |
areaData = fileName.readlines() |
|
|
5 |
for counter in range(0,30): |
6 |
areaData[counter] =
areaData[counter].strip() |
7 |
areas[counter] = areaData[counter] |
|
|
8 |
fileName.close() |
|
|
9 |
def totalScore(score): |
10 |
index = 0 |
|
|
11 |
for index in range (0,5): |
16 |
print(areas[index]) |
17 |
userGuess = input("Enter Guess:
") |
|
|
18 |
if userGuess == areas[index]: |
19 |
score = score + 1 |
|
|
20 |
return score |
|
|
21 |
filename = "" |
22 |
areas = [""]*30 |
Q1: Why has a user-defined
function been used in this code?
|
Q2: Explain how the user-defined function could
be called into action.
|
Q3: What
is the purpose of passing parameters in and out of a user-defined function?
|
Q4: State
what happens to the parameter score
in lines 9 and 20 when passed in and out of the user-defined function.
|
Q5: What is the purpose of file
operations?
File operations allow the programmer
to open, create, read, write, close files, they can save data into sequential files |
Get Free Quote!
301 Experts Online