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

Ashley Keeling
Ashley Keeling
11,475 Points

it says done is not defined please help

import random
import os
import sys

Words=["sunny",
       "cold",
       "computer",
       "speaker",
       "mat",
       "apple",
       "figue",
       "magnet",
       "keyboard",

]

def clear():
    if os.name =="nt":
        os.system("cls")
    else:
        os.system("clear")

def gap():
    print("")

def draw():

    clear()


    print("strikes:{}/5".format(len(bad_guess)))


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


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

def get_guess(bad_guess,good_guess):

    while True:

        guess=input("guess a letter ").lower()

        if len(guess) !=1:
            print("you can only ges a single letter")
            continue
        elif guess in bad_guess or guess in good_guess:
            print("you have already used that letter")
            continue
        else:
            return guess

def play(done):
    clear()
    Random_Word=random.choice(Words)
    bad_guess=[]
    good_guess=[]

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

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

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

                if found:
                    print("YOU WIN!! ")
                    print("The word was {} ".format(Random_Word))
                    done= True

        else:
            bad_guess.append(guess)
            if len(bad_guess)==5:
                draw(bad_guess,good_guess,Random_Word)
                print("YOU LOST!! ")
                print("The word was {} ".format(Random_Word))
                done=True

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

def welcome():
    yes_or_no=input("world you like to play this game('yes' or 'no')")
    if yes_or_no=="yes":
        done=False
    else:
        done=True

print("Welcome to letter guessing game!! ")
while True:
    clear()
    welcome()
    play(done) 

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 55,474 Points

The issue is in the lines:

while True:
    clear()
    welcome()
    play(done) 

The function welcome sets the local variable done but since there is no return statement the default None is returned and the global variable done is not defined.

When the line play(done) is executed, done is still not defined.

Perhaps, you wanted to use the value returned by welcome as the argument to play(). Once solution is to add a return done as the last statement in the welcome() function. Then change the above lines to:

while True:
    clear()
    response = welcome()
    play(response) 

Post back if you need more help. Good luck!!

Ashley Keeling
Ashley Keeling
11,475 Points

I changed the last lines and it says this:

Welcome to letter guessing game!! world you like to play this game('yes' or 'no')yes

Traceback (most recent call last): File "C:/Users/Ashley/personal stuff/python/treehouse/letter game/fgf.py", line 110, in <module> play(response) File "C:/Users/Ashley/personal stuff/python/treehouse/letter game/fgf.py", line 68, in play draw(bad_guess,good_guess,Random_Word) TypeError: draw() takes 0 positional arguments but 3 were given

Chris Freeman
Chris Freeman
Treehouse Moderator 55,474 Points

():

The `draw` function is called using:

```Python
draw(bad_guess,good_guess,Random_Word)

but the function draw is defined as:

def draw():  # no arguments accepted 

The function definition needs parameter defined to accept the arguments passed in by the function calling statement. In other words, how are bad_guess, good_guess , and Random_Word defined within the draw function?

Post back if you need more help. Good luck!!!