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 Collections (2016, retired 2019) Sets Out Of This Word Game

nakalkucing
nakalkucing
12,964 Points

In Better with Set(ter) the code Kenneth uses in the video is different from the one he made back in python basics.

When I run my old code from Python Basics it works. But when I run that code with the changes he made in this video I get back a whole lot of errors. As I was watching I noticed that when he began some of the names were different ie. 'good_guesses' became 'correct' and 'bad_guesses' became 'misses'. No big deal until you get an error saying 'correct is not defined'. But I also noticed that some of his coding was different as he scrolled down the page. Unfortunately, without being able to see his whole code I don't know what to change to make my code work. I would really appreciate any help. Thanks :) Here's my code:

import os
import random
import sys

# make a list of words
words = [
    'colt',
    'filly',
    'foal',
    'mare',
    'horse',
    'stallion',
    'martingale',
    'saddle',
    'gelding',
    'bale',
    'flake',
    'bit',
    'reins',
]

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

def draw(bad_guesses, good_guesses, secret_word):
    clear()

    print('Strikes: {}/7'.format(len(bad_guesses)))
    print('')

    for letter in bad_guesses:
        print(letter, end='')
    print('\n\n')

    # draw guessed letters, spaces, and strikes
    for letter in secret_word:
        if letter in good_guesses:
            print (letter, end='')
        else:
            print ('_', end='')

    print('')

def get_guess(guess):
    while True:
        # take guess
        guess = input ("Guess a letter: ").lower()

        # print ot win/lose
        if len(guess) != 1:
            print ("You can only guess a single letter!")

        elif guess in bad_guesses or guess in good_guesses:
            print ("You've already guessed that letter!")

        elif not guess.isalpha():
            print ("You can only guess letters!")

        else:
            return guess


def play(done):
    clear()
    secret_word = random.choice(words)
    bad_guesses = set()
    good_guesses = set()
    word_set = set(secret_word)

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

        if guess in word_set:
            good_guesses.add(guess)
            if not word_set.symmetric_difference(good_guesses):
                print("You win!")
                print("The secret word was {}.".format(secret_word))
                done = True
        else:
            bad_guesses.add(guess)
            if len(bad_guesses) == 7:
                draw(bad_guesses, good_guesses, secret_word)
                print("You lost! Time next luck better. :)")
                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()


def welcome():
    print('Welcome to Letter Guess!')

    start = input("Press enter/return to start or Q to quit: ").lower()
    if start == 'q':
        print("Bye!")
        sys.exit()
    else:
        return True

done = False

while True:
    clear()
    welcome()
    play(done)
nakalkucing
nakalkucing
12,964 Points

Hey, Steven Parker. I have had this question up for about 5 days. I still can't figure out what I'm doing wrong. Could you give me a few pointers. Thanks for answering my earlier question, Nakal

1 Answer

Steven Parker
Steven Parker
231,236 Points

To be able to access a variable from more than one function, it needs to be declared in the global scope. To do this, you could create them outside of any function, or you could just add this to the beginning of the "play" function:

    global bad_guesses, good_guesses
nakalkucing
nakalkucing
12,964 Points

Thanks! It works without a hitch now. :)