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) Letter Game App Letter Game Refinement

Problems to understand how the functions def draw and def get_guesses get the parameters passed to them

Hello Everyone!

I am having a hard time trying to understand where the parameters for def draw(bad_guesses, good_guesses, secret_word): clear() and def get_guess(bad_guesses, good_guesses): are coming from in the letters game.

As far as I understood if a function has 3 parameters, all 3 of them must be passed in order to have it working, however, in the game, the "draw" function works even though we are guessing 1 letter at time (which means only 1 parameter is being passed right?).

Could anyone please clarify this to me? It is driving me crazy!

Thanks!

Edit: Are we passing the variables themselves as parameters? Is that way he uses def get_guess(bad_guesses, good_guesses)"?

Because on the earlier videos, we had: def lumberjack(name, pronoun):

and then we called it using lumberjack("name", "he's")

Does this means that when creating a function with parameters i can use a variable that already exists? Like this:

example1 = hi
example2 = hey

def new_func(example1, example2):
    print("{} mon, {} dad".format(example1, example2)

new_func()
Fergus Clare
Fergus Clare
12,120 Points

Hello Gabriel! Great question.

A list, whether empty or full, always has a value and is an object. As we move through the guesses, we are adding a letter value to either good_guesses or the bad_guesses list. However, because the value of an empty list still has a value (weird I know!), the function can successfully be executed. You can test this by using the following code:

good_guess = []
bad_guess = []

def draw(good_guess, bad_guess):
   print("{} {}".format(good_guess, bad_guess))

draw(good_guess, bad_guess)
# when we run the program, our results will be
# >>> [] [] 

Hi Fergus, thanks a lot for your response!

One more question though:

when we created the function

def get_guess(bad_guesses, good_guesses):

the arguments have the name of the variables, I tried to modify this to

def get_guess(arg1,arg2):

but i get an error. My understanding is: you can name your arguments whatever, because what is important is passing the value we want when calling the function. I really can't get why changing the name of the args here breaks it. Wouldn't the code below work correctly?:

def get_guess(arg1,arg2):
   print("1 {}, 2 {}".format(arg1,arg2))

get_guess(bad_guesses, good_guesses)

Once again i really appreciate the help!

1 Answer

AJ Salmon
AJ Salmon
5,675 Points

Hey Gabriel!

To answer your second question: yes. The reason he uses the actual variable names in the parameter is simply for clarity, and your code would work perfectly. If you don't use the actual variable names in the parameter, I'd suggest making a note documenting your thought process so that you remember exactly what to pass to the argument when you call it later on. Hopefully this clears it up! Happy coding :)

AJ

Hello AJ!

Thanks a lot for the answer, now it is clear to me what happens!

AJ Salmon
AJ Salmon
5,675 Points

My pleasure! I'm glad you're comfortable with the concept.