Miguel O.Python Skills Homework 3
Miguel O.Python Skills Homework 3
It can be helpful to have a block of code that is separate to the main program – this is called
a subroutine. It can often be helpful to decompose a problem by identifying the small-
medium sized tasks and separating them out in order to tackle them individually. The main
program will then call each one as and when it needs to.
[5]
2. Study the program and then answer the questions below it.
def getGuess():
guess = 0
while guess < 1 or guess > 100:
guess = int(input("Enter a guess between 1 and 100: "))
return guess
def displayRules():
print("This is a guessing game.")
print("You have to guess the number between 1 and 100 \
chosen by the computer")
def playGame(target):
guessedNumber = False
guess = getGuess()
while guess != target:
if guess > target:
guess = int(input("Too high - guess lower: "))
elif guess < target:
guess = int(input("Too low - guess higher: "))
else:
guessedNumber = True
print("\nThat's correct, well done!")
return guessedNumber
#main
choice = "0"
while choice != "3":
print("\nMain Menu")
print("1. Display the rules")
print("2. Play the game")
print("3. Quit")
choice = input("Choose an option: ")
if choice == "1":
1
Homework 3
Practical programming skills in Python
displayRules()
elif choice == "2":
guessedNumber = playGame(random.randint(1,100))
elif choice !="3":
choice = input("Invalid choice - must be between 1 and 3: ")
input("Goodbye ... press Enter to Quit program ")
line 1 to line 5
(b) Identify a call statement which does not pass a parameter, but which accepts
a return value from the function. [1]
line 12 to line 23
(c) Explain, in general terms, what the value of the parameter target in the function
playGame() is and how it is assigned this value. [3]
target is the number that the user needs to guess it is assigned randomly from 1 to
100___________________________________________________________________
__
_____________________________________________________________________
_____________________________________________________________________
3. Write an explanation of how subroutines can be used by programmers and how this can
help the development process.
You will be assessed on your use of written language and technical terms. [5]
Subroutines are smaller programs coded inside a larger program, meaning that they are
inside it to perform a specific task. This may be needed more than once inside the program.
They are smaller so they are easier to write, can be saved as separate modules, and can
be repeated at various points of the program.
[Total 15 marks]