Activity 1
Creating a sub-routine in Python involves some new syntax that you will need to get used to.
Copy and paste the code below into Python:
def menu(): # defines the sub-routine with the name as menu
print ("Here is my menu") # lines of code that will run when called
print ("Isn't it nice?")
menu() # calls the menu sub-routine
The code contained in the sub-routine will only run when it is called. The sub-routine is called on the last line of the example: menu()
Activity 2
To add another sub-routine we need to use the same syntax. Below is the original code with an additional sub-routine.
def menu():
print ("Here is my menu")
print ("Isn't it nice?")
print ("Press enter to play the game") ### new line of code
if input()=="": ### runs the play sub-routine if enter is pressed
play()
def play():
print ("This is my awesome game, do you like it?")
menu()
Adapt your current piece of code with this one to see how sub-routines can work with each other.
Activity 3
Try to make a maze of sub-routines to help you practice the syntax and get used to it.
For example,
Make lots of sub-routines that link to each other like the example above. The user has to press a specific letter on the keyboard to get to the next sub-routine.
Activity 4
You should be comfortable writing sub-routines now. If you aren't then do some more practice before attempting this activity.
PROBLEM: A game with a menu
Think of a game that you could create in Python. A guess the word game is a good one to try.
The game will need a menu.
The menu must allow the user to:
Use your computational thinking skills to solve this problem.
If you are unsure how to program this, you may need to re-visit cycle 7_3.
Create a binary arithmetic maths program for students to practice their binary addition and subtraction.
The program should:
You may want to re-cap 7_3 with the students before trying this activity.