[Tutor] variables within function - wha?

D-Man dsh8290@rit.edu
Thu, 19 Jul 2001 12:07:35 -0400


On Thu, Jul 19, 2001 at 10:17:24AM -0700, kromag@nsacom.net wrote:
| I am trying to get a function to perfrom simple calculations based on 3 
| values.
| 
| -----------------------------------
| 
| def filter(cc,rpm,type):
| 	cid=cc/16.387
| 	ocg=(cid*rpm)/20839
| 	foam=ocg*1.3767
| 	paper=ocg*1.2181
| 	if type==paper:
| 		return paper,
| 	elif type==foam:
| 		return foam,
| 	else:
| 		return ocg
| 
| -----------------------------------
| 
| I can't figure out why:
| 
| >>> filter(1800,7000,foam)
| Traceback (innermost last):
|   File "<pyshell#1>", line 1, in ?
|     filter(1800,7000,foam)
| NameError: There is no variable named 'foam'
| >>> 
| 
| occours. Have I been inhaling too much brake cleaner?

(this is in addition to the earlier response)

You aren't in the function yet.  You want to invoke it with
    filter( 1800 , 7000 , 'foam' )
(I like the extra spaces)

Also, filter is a built-in function so I recommend changing the name
to something else.  In addition it is probably a good idea to make a
"global" constant for FOAM and PAPER rather than duplicating string
literals in several places (use the same "global" in the comparison in
the function).  Then you can change the value to anything you want and
clients won't know or care that there was a difference.

HTH,
-D