Subroutines & Procedures
Learning Objectives:
Building- that you have used the Functions 'input' and 'print'
Linking and Using - how its useful to make your own functions
Extending
Subroutines are a great way of organising your code and are the key to good coding
You can create a function and run it as many times as you want anywhere in your code. Variables you create in a function are called local variables and only work in that function.
Variables that are in your main body of code, called global variables, won't work in a Procedure or function, although there are ways round this.
You can create a function and run it as many times as you want anywhere in your code. Variables you create in a function are called local variables and only work in that function.
Variables that are in your main body of code, called global variables, won't work in a Procedure or function, although there are ways round this.
ADVENTURE GAME:
#def is used to define the Procedure or function
# This uses Chap1() to error catch/validate the input option as it will run the subroutine again if the option is not "a" OR "b" |
from time import sleep
import webbrowser def chap1(): print("""intro""") sleep(0.5) option = input("a) or b) ").lower() if option == "a": print ("Sad news...") death() elif option == "b": print ("""Lucky""") chap2() else: print ("that was not an option") chap1() def death(): print("You die!") webbrowser.open("http://vomzi.com/wp-content/uploads/2015/11/head-exploding-gif-tim-and-eric.gif") def main(): chap1() main() |
Challenge:
- Copy the code above
- Add a story
- Add 2 more chapters
EXTEND YOURSELF:
Continue your online course @ Codecademy