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

Difficulty understanding which arguments to pass through a function

I have always struggled with the concept of which argument I should/need to pass through a function. Case and point - I am working through the python track, and there is an exercise for making a spelling game. Here is a snippet of the code:

def play(done):
    clear()
    secret_word = random.choice(words)
    bad_guesses = []
    good_guesses = []

    while True:
        draw(bad_guesses, good_guesses, secret_word)
        guess = get_guess(bad_guesses, good_guesses)

        if guess in secret_word:
            good_guesses.append(guess)
            found = True
            for letter in secret_word:
                if letter not in good_guesses:
                    found = False
            if found:
                print("You win!")
                print("The secret word was {}".format(secret_word))
                done = True
        else:
            bad_guesses.append(guess)
            if len(bad_guesses) == 7:
                draw(bad_guesses, good_guesses, secret_word)
                print("You lost!")
                print("The secret word was {}".format(secret_word))
                done = True

        if done:
            play_again = input("Play again? Y/n ").lower()
            if play_again != 'n':
                return play(done=False)
            else:
                sys.exit()

Why was 'done' passed through the function? Why not 'found', for example? Or 'good_guesses' etc?

2 Answers

This is honestly an interesting program. The done argument is for the function to know whether it should run itself again. This is honestly a confusing function argument example - if you are simply curious about function arguments. The done argument in this function is used farther down the while loop AFTER it tries to find the correct guess. if done is False 7 times (based on len(bad_guesses) == 7), it will get set to True and the if done: logic will be called. If the user types n in the console after that, the function calls itself again with done = False, thus starting the game over again. By calling the function again, it will reset all of the variables and start over. Hope that helps. If not let me know what's still confusing.

Thanks Rick!

Yes it was probably not a the best example. I am just fuzzy on what exactly the purpose of an argument is. Is it just to let the function know whether to run itself again?

Arguments serve many purposes. They can be used for a myriad of situations! They are there to help a function perform whatever actions it may need to do. Here are some examples:

def print_age(age):
  print("You are {}".format(age))

OR

def add_money_to_account(amount):
  self.total_money += amount
  print("You have ${}".format(self.total_money))

Don't get too caught up in the self piece of the above code, focus more on how the argument helped the function do it's job. In the first example, it used age to print out your age in a sentence on the screen. In the second example it used the amount variable to add to a total_money variable. Let me know if that helps.

Thanks Rick, this helps a lot.