Functions Not Fun (yet)-please help!

Joe Goldthwaite joe at goldthwaites.com
Sun Jan 16 14:51:04 EST 2011


Hi Kathy,

 

The defaults only get assigned when you leave them out of the list. This
will work the way you want by setting b & c to the defaults.

 

            print my_func(a)

 

When  you try this;

 

            a = "testing"

            b = "defaults"

            print my_func(a, b, c)

 

You get an undefined error on c.  This is because at this point, c has been
created but hasn't had anything assigned to it.  In other words, you're
actually passing the undefined c variable from here into my_func.

  _____  

From: python-list-bounces+joe=goldthwaites.com at python.org
[mailto:python-list-bounces+joe=goldthwaites.com at python.org] On Behalf Of
Cathy James
Sent: Sunday, January 16, 2011 7:49 AM
To: python-list at python.org
Subject: Functions Not Fun (yet)-please help!

 

Dear all,

 

I can't thank you enough for taking time from your busy schedules to assist
me (and others) in my baby steps with Python. Learning about functions now
and wondering about some things commented in my code below. Maybe someone
can break it down for me and show me why i cant print the function i
created. I am using IDLE, saved it as .py

 

def my_func(a, b="b is a default" ,c="c is another default"):
    print (a)
    print (b)
    print (c)
    

#printing the function itself:
    
#1. assign value to a only, b and c as default:
a= "testing"
print (my_func(a,b,c)) #why does program say c is not defined, tho default
in function above?
#2. assign a and b only, c is default
print my_func(a="testing a", b="testing b")

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110116/00fdbce5/attachment.html>


More information about the Python-list mailing list