#!/usr/bin/env python # coding: utf-8 # In[4]: # Section 5.2 continued # Can define multiple functions in the same program # no problem! def main(): print("This is my program.") message() print("Bye!") def message(): print("Hello") print("World!") main() # In[12]: # Section 5.3 -- designing with functions # first thing is a flowchart for the previous program # we put this on the board # # Example program: a dryer company hires you to write a # step-by-step evaluation program for fixing dryers of # a certain brand. The steps they want are as follows: # Step 1. Unplug and move the dryer from the wall # Step 2. Remove the six screws from the back. # Step 3. Remove the back panel. # Step 4. Pull top part straight out. # # requirements: each step to be displayed one at a time, # and user should hit enter to proceed to the next step # # Goal here: write each step as a function and then # wrap those functions into an order. def main(): # display a welcome message and wait for user input start_message() input("Press enter to continue.") # display step 1 & wait step1() input("Press enter to continue.") # display step 2 and wait step2() input("Press enter to continue.") # display step 3 and wait step3() input("Press enter to continue.") # display step 4 and wait step4() def start_message(): print("Welcome to the repair utility for the XR123 dryer.") def step1(): print("Unplug and move the dryer from the wall") def step2(): print("Remove the six screws from the back.") def step3(): print("Remove the back panel.") def step4(): print("Pull top part straight out.") main() print(type(main())) # In[15]: # Section 5.4 -- local variables # Variables defined within a function cannot be retrieved # from outside of the function. # # Essentially -- variables are destroyed when a function # completes its steps. # # The following example illustrates the point: def main(): get_name() print("Hello for the second time",name) def get_name(): # define variable name and get input from user for it name=input("Enter your name!") print("Hello for the first time",name) main() # notice in the above code that the "Hello for the first time" # print command works as expected -- its printed the user # inserted name without problem. HOWEVER, the "Hello for the # second time" print command fails to work as expected -- # it gives an error!! #This is because the "name" variable is destroyed when the # get_name() function is called and completes. So the # variable is not accessible except within that function's code. # So we say that the variable "name" occurring in the get_name() #function is a "local variable" -- it is "local" to the get_name() # function. We say that this "name" variable is "in the scope" # of the get_name() function. Here, "scope" is referring to # the occurrence of it within that function's statements. # In[29]: # Bird example from the book # The same variable is used in different functions without problem. def main(): texas() birds=5 westvirginia() texas() westvirginia() def texas(): birds=10000 print("There are",birds,"birds in Texas.") def westvirginia(): print("There are",birds,"birds in West Virginia.") # uncommenting the following line causes an error # because the interpreter thinks you made a mistake # birds=2000 main() # In[ ]: