Presentation is loading. Please wait.

Presentation is loading. Please wait.

Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.

Similar presentations


Presentation on theme: "Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace."— Presentation transcript:

1 Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace get there before compilation started? Assume there are no syntax errors. In the follow code, assume the user types 2 when prompted. Notice that we are using an int value for the price of coffee in this version.

2 Show what happens at each step during compilation and runtime… …and before

3 RAM start IDLE

4 RAM 5 4 3 2 1 start IDLE – small integers and single character strings are loaded into RAM

5 __builtin__ module is imported RAM 5 4 3 2 1 namespaces builtin len print round global

6 compilation proceeds from top to bottom in source file – line 1. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME

7 line 1. global constant PRICE_PER_CUP added to global namespace and bound to 1 code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME

8 line 2. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME

9 line 2. the function name added to global namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME

10 line 3. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME

11 line 3. docstring added to help system code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

12 line 4. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

13 line 4. RHS looks fine – no searching is done code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

14 line 4. no input, no function calls code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system screen THERE WILL BE NO OUPUT UNTIL RUN TIME NO INPUT ''' returns user's coffee order quantity '''

15 line 4. cups is added to getNumCoffeeCups’ local namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME NO INPUT ''' returns user's coffee order quantity '''

16 line 4. no assignment is done – assignment is a runtime activity, no arrow appears code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME NO INPUT ''' returns user's coffee order quantity '''

17 line 5. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

18 line 5. looks like a return statement – no searching is done. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

19 line 5. no value is returned – this is compile time. returning a value is a run time activity code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

20 getNumCoffeeCups’ compiled body is placed into RAM, ready to be called – no run code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

21 line 6. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

22 line 6. the function name is added to global code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME ''' returns user's coffee order quantity '''

23 line 6. parameter name is added to calcOwed’s local namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity '''

24 notice that there are two “cups” names in two different namespaces – different scopes code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity '''

25 getNumCoffeeCups’s creator chose a good name. calcOwed’s creator chose a good name. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity '''

26 calcOwed’s creator doesn’t even know that any other functions exist code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

27 line 7. – docstring added to help system code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

28 line 8. looks like an arithmetic expression and a return statement – no searching is done code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

29 calcOwed’s compiled body is placed into RAM, ready to be called – no run happens code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

30 line 9. code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

31 line 9. – function name is added to global namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

32 line 9. – parameter names are added to showBill’s local namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

33 line 10. – docstring is added to help system code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

34 lines 11. and 12. – print() looks like a function call – no searching is done code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

35 lines 11. and 12. – those str()’s look like a function calls no searching is done code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

36 lines 11. and 12. – str expression compiles – everything is fine code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

37 showBill’s compiled body is placed into RAM, ready to be called – no execution, no run code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

38 notice that there are three “cups” names in three different namespaces – different scopes code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

39 again, no conflict because the three ‘cups’s are in different scopes – different namespaces code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

40 line13. the function name: main is added to the global namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

41 there is no docstring written under main’s def line – the program level docstring is at the top code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

42 line 14. no function call happens – this is compile time, function calls happen at run time code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

43 line 14. getNumCoffeeCups() looks like a function call so compiler is happy code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

44 line 14. the assignment statement does not happen – it will at run time, not now code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

45 line 14. cups is added to the main’s local namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

46 line 15. again no function call happens – it looks like a function call - fine code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

47 line 15. no search is made to see if cups already exists – that will be done at run time code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

48 line 15. the assignment statement is not executed code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed cups ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

49 line 15. owed is added to main’s local namespace – stil no name conflicts! code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

50 line 16. looks like a function call – no searching happens – no call happens code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

51 main’s compiled body is placed into RAM, ready to be called – no execution, no run code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen THERE WILL BE NO OUPUT UNTIL RUN TIME cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

52 compile time has ended – now we enter exectution or run time at the call of main() code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups screen cups cups owed ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

53 compile time has ended – now we enter exectution or run time at the call of main() code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

54 run time order is the order of function calls and returns and starts with the call to main() code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

55 the name main is searched for – LEGB and found –NameError does not occur code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

56 if there is no communication through parameters, execution continues in the body of main code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

57 the RHS of the assignment is evaluated – it’s a function call, so… code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

58 …the name getNumCoffeeCups is searched for LEG and found in the global namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

59 since there is no communication via parameters, the first line in getNumCoffeeCups runs code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

60 raw_input is searched for – LEGB finally found in the builtin namespace – no NameError code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

61 raw_input is passed the prompt string and it shows it to the user code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

62 the user hits the 2 button, hits the Enter button, the newline is trashed, the str ‘2’ is returned code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

63 the name int is searched for – LEGB and found in the builtins code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

64 the returned ‘2’ is passed to int(_) which returns the int 2 which is now the value of the RHS code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

65 now that the RHS’s value is fully known, the run time system looks to see if the 2 is in RAM code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

66 the value of the int(raw_input(“..”)) function call expression becomes the returned 2 code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

67 the value of the int(raw_input(“..”)) function call expression becomes the returned 2 code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

68 the LHS, cups, is searched for: L – found in the local namespace of getNumCoffeeCups code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

69 cups is made to point at where the 2 is in RAM – cups is bound to the RAM containing the 2 code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

70 notice that there is no confusion about which ‘cups’ is assigned to – it’s scope is local code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? " 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

71 return cups – cups is again searched for and found in the locals of getNumCoffeeCups code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

72 return returns to it’s caller a reference to where the 2 is in RAM code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

73 the value of the function call expression becomes the value returned, the run continues there code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

74 …and all local references back at the call go away – cups stays in the local namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

75 …and all local references back at the call go away – cups stays in the local namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

76 getNumCoffeeCup’s local cups is no longer in scope when its lifetime ended after the return code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

77 main’s local cups is searched for in main’s local and found and the assignment occurs code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

78 main’s local cups is searched for in main’s local and found and the assignment occurs code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''‘ 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

79 line 15. – function call on RHS – calcOwed is found in the globals – no NameError code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

80 cups is searched for and found in main’s locals… code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

81 setting up the call to calcOwed - its parameter name ‘cups’ searched for and found locally code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

82 calcOwed’s local parameter name ‘cups’ is assigned main’s local variable ‘cups’ code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

83 this is pass by asasignment code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

84 now that the communication via parameters has been set up, the body is executed code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

85 the expression to be returned is evaluated, cups is found in calcOwed’s locals code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

86 PRICE_PER_CUP is searched for – LEGB, found in the global namespace code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

87 cups * PRICE_PER_CUP is evaluated and that value is searched for in RAM and found code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

88 the expression becomes that value code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

89 back at the call, the function call becomes the returned value code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

90 and all local references in calcOwed go away – they are no longer bound to values in RAM code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

91 and all local references in calcOwed go away – they are no longer bound to values in RAM code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

92 execution continues on the line with the call code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

93 owed is searched for and found in main’s local namespace… code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

94 …and the assignment is made code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

95 we’ll speed things up a bit now code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

96 find showBill in the globals, cups and owed in the main’s locals code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

97 find showBill’s parameters code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

98 pass by assignment code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

99 find print and str in builtins, look up owed’s and cups’ values code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

100 the string is built, looked for in RAM, not found, stored in RAM somewhere, passed, printed code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 You owe $2 for your 2 cup(s) ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

101 execution returns to the caller: main(), local references in showBill go away, names stay code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 You owe $2 for your 2 cup(s) ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''

102 main() call is over, local references in main go away, execution ends – WHEW! code 1. PRICE_PER_CUP = 1 # in US$ 2. def getNumCoffeeCups(): 3. ''' returns user's coffee order quantity ''' 4. cups = int( raw_input( "How many cups? “ ) ) 5. return cups 6. def calcOwed( cups ): 7. ''' returns calculated amount owed ''' 8. return cups * PRICE_PER_CUP 9. def showBill( cups, owed ): 10. ''' presents bill nicely ''' 11. print( "You owe $" + str( owed ) + "\n" + \ 12. "for your " + str( cups ) + " cups(s)" ) 13. def main(): 14. cups = getNumCoffeeCups() 15. owed = calcOwed( cups ) 16. showBill( cups, owed ) 17. main() namespaces builtin len print round global PRICE_PER_CUP getNumCoffeeCups local calcOwed local showBill local main local RAM 5 4 3 2 1 the help system cups cups owed screen How many cups? 2 You owe $2 for your 2 cup(s) ''' returns user's coffee order quantity ''' ''' returns calculated amount owed ''' ''' presents bill nicely '''


Download ppt "Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace."

Similar presentations


Ads by Google