Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python Fully Functional

Where does the name variable come from and how are they connected?

I don't understand how the name variable is created and connected with the rest of the script.

def hows_the_parrot():
    print("He's pining for the fjords!")

hows_the_parrot()

def lumberjack(name):
    if name.lower() == 'kenneth':
        print("Kenneth's a lumberjack and he's OK!")
    else:
        print("{} sleeps all night and {} works all day!".format(name, name))

lumberjack("Nate")

1 Answer

The name variable inside the function is just a placeholder for whatever gets passed in. Whatever you pass into the function will be assigned to the name variable

def lumberjack(name):  #until it's called, it's just a placeholder variable
    if name.lower() == 'kenneth':
        print("Kenneth's a lumberjack and he's OK!")
    else:
        print("{} sleeps all night and {} works all day!".format(name, name))

lumberjack("Nate") # Now we're calling on lumberjack and passing in "Nate" 
              # lumberjack looks at this and assigns "Nate" to the first variable, name

Thanks!