8_1_8

WHAT: Develop a solution to a problem that uses sub-routines

SMART Start:

Define the term sub-routine in five words.

Extra Mile:

Explain why programmers use sub-routines to help them solve problems.

HOW:

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:

  • Enter their name (then return to the menu)
  • Play the game (then return to the menu)
  • View a Score Board (then return to the menu)
  • Quit the game

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.

CHECK:

EMBED:

Create a binary arithmetic maths program for students to practice their binary addition and subtraction.

The program should:

  • Have a menu
  • Store the student score
  • Let the user choose addition or subtraction questions
  • Check their answers and tell them if they are correct or not

CLASSROOM IDEAS:

You may want to re-cap 7_3 with the students before trying this activity.