Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Letter Game App Letter Game Refinement

Atıl Kurtulmuş
Atıl Kurtulmuş
4,945 Points

Arguments and booleans in a function

I have no past coding experience and I started learning with python. Treehouse is a great website and I love it. I believe I'm making some progress but there is one thing, so far, I cannot get a grasp of. Why do some specific arguments and booleans go into a function?In the code below, for example, why do we have to inclue list names as arguments in functions like draw and get_guess and done boolean into play function?

import random
import os
import sys

words = ["elma","armut","karpuz"]


def draw(bad_guess, good_guess, word):

    print("Yanlış tahmin sayısı: {}/10".format(len(bad_guess)))
    print("")

    for letter in bad_guess:
        print(letter, end=" ")
    print("")

    for letter in word:
        if letter in good_guess:
            print(letter, end=" ")
        else:
            print("_", end=" ")
    print("")

def get_guess(bad_guess, good_guess):
    while True:
        guess = input("Bir harf tahmin et: ").lower()

        if len(guess) != 1:
            print("Sadece tek bir harf tahmin edebilirsin")

        elif guess in bad_guess or guess in good_guess:
            print("Bu harfi daha önce tahmin ettin")

        elif not guess.isalpha():
            print("Sadece harf tahmin edebilirsin")

        else:
            return guess

def play(done):
    word = random.choice(words)
    bad_guess = []
    good_guess = []

    while True :
        draw(bad_guess, good_guess, word)
        guess = get_guess(bad_guess, good_guess)

        if guess in word:
            good_guess.append(guess)
            found = True

            for letter in word:
                if letter not in good_guess:
                    found = False

            if found:
                print("Kazandın!")
                print("Doğru kelime: {}".format(word))
                done = True

        else:
            bad_guess.append(guess)
            if len(bad_guess) == 10:
                draw(bad_guess,good_guess,word)
                print("Kaybettin.")
                print("Doğru kelime: {}".format(word))
                done = True
        if done:
            tekrar = input("Tekrar oyna E/H").lower()
            if tekrar != h:
                return play(done= False)
            else:
                sys.exit()


def welcome():
    başla = input("Başlamak için Enter'a ya da çıkmak için  Q'ya bas").lower()
    if başla == "q":
        sys.exit
    else:
        return True

done = False

while True:
    welcome()
    play(done)
Zach Anderson
Zach Anderson
3,691 Points

If I understand correctly what you're asking about is the difference between local and global variables and function parameters. I believe this topic gets covered later in the course (although I'm not there yet) but here's a super quick version:

(Real quick, I should let you know that boolean is just a datatype. Variables can be integers, floats, strings, booleans, or a whole bunch of other things but I don't think it exactly ties in to your main question.)

A global variable is any variable defined outside of a function. In your code the variable words is global. That means whenever you use the variable words it will always use that same variable.

A local variable is created temporarily each time the function is run. So when you defined the function draw with three parameters (which is data that you give to draw() to use as local variables) the timeline sorta looks like this:

  • Your program (presumably) calls draw() at some point
  • Whatever got passed into draw() gets stored in these local variables. In this case, the first thing that you give to draw() gets stored in a local variable called bad_guess, the second thing gets stored in good_guess, and the last thing gets stored in word
  • The code that you defined inside draw() then runs using those values. Note that since draw may get called later on in the program or it may get run many times you can use different values for each of those three parameters
  • When all of the code inside draw() has been run it discards all three of those variables. So if draw() finishes and another line of code goes to use bad_guess it won't be there*

def function_name(parameter1, parameter2, etc.):

Here's another way of looking at it:

draw('value1', 23, 'yay!')

will run the function code that you defined in draw(). When it runs that code it will pop 'value1' into a local variable called bad_guess and 23 will get put into a local variable called good_guess and 'yay!' will be put into word. Basically doing this:

bad_guess = 'value1'

good_guess = 23

word = 'yay!'

The beauty of this is it lets you run code where you don't know what bad_guess is, or in this case you're running code where bad_guess will be changing, so you can't just write in bad_guess = 'value1'

*(unless you've got a global variable named bad_guess as well, but I wouldn't recommend doing that!)

...hopefully this explanation helps somewhat... I tried to make it simpler but I may just be more confusing ? Like I said I haven't gotten to global/local variables yet (though apparently I've some passing familiarity) so I'm hoping what I said is accurate.

Cheers, -Zach

1 Answer

Atıl Kurtulmuş
Atıl Kurtulmuş
4,945 Points

Many thanks for your answer! You made it very clear. I think i'm starting to understand how building a function works after a lot of messing around with pycharm. Seeing what the function really does in practice after you write it helps a lot for understanding the basics.