Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> #Can define multiple functions in the same program >>> def main(): print("bla bla bla") message() print("Bye!!") >>> def message(): print("Hello!") print("World!") >>> main() bla bla bla Hello! World! Bye!! >>> # Section 5.3 -- Design with functions >>> # draw a flow chart for functions (on board: we will draw the chart for the previos program) >>> # >>> # >>> # Step-by-step instructions for employees of a dryer repair company >>> # Here are the steps: >>> # Step 1. Unplug the dryer and move it from the wall. >>> # Step 2. Remove the 6 screws from the back of the dryer. >>> # Step 3. Remove the back panel. >>> # Step 4. Pull the top part straight out. >>> # >>> # >>> # You're told that you need to display each step one at a time. >>> # And you need to hit enter to reach the next step. >>> # >>> # >>> def main(): start_msg() step1() step2() step3() step4() >>> def start_msg(): print("Welcome to the repair utility for the XRLT Acme Dryer") >>> def step1(): print("Step 1. Unplug the dryer and move it from the wall.") input("Hit enter to continue.") >>> def step2(): print("Step 2. Remove the six screws from the back.") input("Hit enter to continue.") >>> def step3(): print("Step 3. Remove the back panel.") input("Hit enter to continue.") >>> def step4(): print("Step 4. Pull the dryer straight out of the top.") >>> main() Welcome to the repair utility for the XRLT Acme Dryer Step 1. Unplug the dryer and move it from the wall. Hit enter to continue. Step 2. Remove the six screws from the back. Hit enter to continue. Step 3. Remove the back panel. Hit enter to continue. Step 4. Pull the dryer straight out of the top. >>> # Section 5.4 -- Local Variables >>> # This example will fail. >>> def main(): get_name() #this function will define a variable named name from user print('Hello',name) >>> def get_name(): name=input('Insert your name.') >>> main() Insert your name.Tom Traceback (most recent call last): File "", line 1, in main() File "", line 3, in main print('Hello',name) NameError: name 'name' is not defined >>> # that failed because the variable "name" was local to the get_name() function >>> # local variables are always destroyed when the function terminates execution >>> # >>> # >>> # consequence: local variables can repeat without problem >>> # Example (birds) >>> def miain(): KeyboardInterrupt >>> KeyboardInterrupt >>> def main(): texas() westvirginia() >>> def texas(): birds=5000 print("There are",birds,"birds in Texas.") >>> def westvirginia(): birds=100 print("There are",birds,"birds in West Virginia.") >>> main() There are 5000 birds in Texas. There are 100 birds in West Virginia. >>> # Alter these functions a little >>> def main(): birds=5 texas() westvirginia() >>> # alter the westvirginia() function >>> def westvirginia(): print("There are",birds,"birds in West Virginia.") >>> main() There are 5000 birds in Texas. Traceback (most recent call last): File "", line 1, in main() File "", line 4, in main westvirginia() File "", line 2, in westvirginia print("There are",birds,"birds in West Virginia.") NameError: name 'birds' is not defined >>> # This didn't work in IDLE (we are using it because the internet is broken). But >>> # it does work in Jupyter notebook. >>> # >>> # Section 5.5 -- Passing Arguments to Functions >>> # An argument to a function is anything in the parentheses when you call the function. >>> # We haven't defined any functions that allow arguments yet. But we will. >>> # Def: A parameter (or "parameter variable") is a special variable assigned to the value >>> # of an argument to a function when the function is called. All parameters are >>> # local variables. >>> # >>> # >>> # Example of a function that doubles its input. >>> def show_double(number): result=number*2 print("The double of",number,"is",result) >>> show_double(5) The double of 5 is 10 >>> show_double(1.2) The double of 1.2 is 2.4 >>> show_double("pilot") The double of pilot is pilotpilot >>> 2*"string" 'stringstring' >>> 2.0*"string" Traceback (most recent call last): File "", line 1, in 2.0*"string" TypeError: can't multiply sequence by non-int of type 'float' >>> # >>> # >>> # Use this function in another function. >>> def main(): value=5 show_double(value) >>> main() The double of 5 is 10 >>> # >>> # >>> # >>> # Convert cups to fluid ounces >>> # >>> def main(): intro() # this will tell user what this program does #Get the number of cups from the user cups_needed=int(input("How many cups do you need to convert to fluid oz?")) cups_to_ounces(cups_needed) >>> def intro(): print("This program converts cups to fluid ounces. For your information:") print("1 cup = 8 fluid ounces") >>> def cups_to_ounces(number): ounces=8*number print("That converts to",ounces,"ounces.") >>> main() This program converts cups to fluid ounces. For your information: 1 cup = 8 fluid ounces How many cups do you need to convert to fluid oz?3 That converts to 24 ounces. >>> # >>> # Let's write a function that takes two arguments. >>> # Write a program that takes two numbers as input and multiplies them. >>> def multiply_function(first_num,second_num): print("The product of",first_num,"and",second_num,"is",first_num*second_num) >>> multiply_function(5,2) The product of 5 and 2 is 10 >>> # We can do the same thing with strings >>> def first_last_name(first_name,last_name): print("Your last name is",last_name,",and your first name is",first_name) >>> first_last_name("Tom","Cuchta") Your last name is Cuchta ,and your first name is Tom >>> first_last_name(3,2) Your last name is 2 ,and your first name is 3 >>>