Why has a user-defined function been used in this code?

computer science

Description

Outcome 1: Question-based task (Python)

The assessment is a question-based task.

 

Assessment Standards 1.1 and 1.2

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

 

 

 

 

 

 


Related Questions in computer science category


Disclaimer
The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.