[Tutor] Function behavior

Ken Green beachkidken at gmail.com
Thu Sep 16 14:18:45 CEST 2010


I am unclear on the behavior of using a function.  Below is a short code 
I wrote to print an amount out after inputting the number of match.

# TEST Function.py

def change(amount):
     if match == 1:
         amount = 0
     if match == 2:
         amount = 0
     if match == 3:
         amount = 3

match = raw_input("How many matches?: ")
change(match)
print amount

ERROR Message:

How many matches?: 2
Traceback (most recent call last):
   File "/home/ken/Python262/TEST Function.py", line 13, in <module>
     print amount
NameError: name 'amount' is not defined

How do I print out the amount of 0 if I input 2?

Should it be def change(match) instead of def change(amount)?

Perhaps, change(amount) instead of change(match)?

Perhaps, I need to add return somewhere?

Thanking you all in advance for your assistance.

Ken


More information about the Tutor mailing list