[Tutor] variables within function - wha?

Remco Gerlich scarblac@pino.selwerd.nl
Fri, 20 Jul 2001 09:59:35 +0200


On  0, 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?

Are you sure you want to give it some value in the variable foam? Or maybe
do you want to give it the *word* foam? Then you need to put quotes around it,
'foam', and in the function as well:

def filter(cc, rpm, type):
   cid = cc/16.387
   ocg = (cid*rpm)/20839
   foam = ocg*1.3767 
   paper = ocg*1.2181 # Here you set the variable to some value
   if type == 'paper': # But I assume you want to test against the *word* paper
      return paper # Return the value in the variable paper
   elif type == 'foam':
      return foam
   else:
      return ocg

Now you can call it with, for instance,
filter(18000,7000, 'foam')

I've also removed the commas after the return statements: with the commas,
you don't return the number, but a 1-element tuple containing the number.

-- 
Remco Gerlich