namespace, scope, compile time activities, runtime activities when do the small integer values get...

Post on 05-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

Show what happens at each stepduring compilation

and runtime……and before

RAM

start IDLE

RAM

5

4

3

2

1

start IDLE – small integers and single character strings are loaded into RAM

__builtin__ module is imported

RAM

5

4

3

2

1

namespaces

builtin lenprintroundglobal

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 lenprintroundglobal

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

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 lenprintroundglobal

PRICE_PER_CUP

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

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 lenprintroundglobal

PRICE_PER_CUP

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

screen

THERE WILL BENO OUPUT UNTILRUN TIME

NO INPUT

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

NO INPUT

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

NO INPUT

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cups

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

THERE WILL BENO OUPUT UNTILRUN TIME

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

screen

cups

cupsowed

cupsowed

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

…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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

…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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

…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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

…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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2You owe $2for your 2 cup(s)

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2You owe $2for your 2 cup(s)

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

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 lenprintroundglobal

PRICE_PER_CUP

getNumCoffeeCups local

calcOwed local

showBill local

main local

RAM

5

4

3

2

1

the help system

cups

cups

cupsowed

cupsowed

screen

How many cups?2You owe $2for your 2 cup(s)

''' returns user's coffee order quantity ''' ''' returns calculated amount owed '''''' presents bill nicely '''

top related