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 Introduction

Not able to get a win (even if i guess all the right letters)

Hello,

I am unable to get to a win, even when I guess all the right letters. My program will look like this: Guess a letter: y
blueberry
Strikes: 0/7

Below is my code:

import random
#make a list of words
words = [
    'apple', 
    'bannana',
    'coconut',
    'strawberry',
    'lime',
    'grapefruit',
    'lemon',
    'kumquat',
    'blueberry',
    'melon'
]
while True:
  start = input("Press enter/return to start or enter Q to quit: ")
  if start.lower() == 'q':
    break

#pick a random word
  secret_word = random.choice(words)
  bad_guesses  = []
  good_guesses = []
  while len(bad_guesses) < 7 and  len(good_guesses) != len(list(secret_word)):
      #draw guessed letters, spaces and strikes
      for letter in secret_word:
          if letter in good_guesses:
              print(letter, end='')
          else:
              print('_', end='')

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

#take guess
      guess = input("Guess a letter: ").lower()
      if len(guess) != 1:
          print("You can only guess a single letter!")
          continue
      elif guess in bad_guesses or guess in good_guesses:
          print("You've already guess that letter !")
      elif not guess.isalpha():
          print("You can only guess letters!")
          continue

      if guess in secret_word:
          good_guesses.append(guess)
          if len(good_guesses) == len(list(secret_word)):
              print("You win! the word was {}".format(secret_word))
              break
      else:
          bad_guesses.append(guess)
  else: 
      print("You didn't guess it! My Secret wird was {}".format(secret_word))      
#print out win/lose

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

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Many have hit this bug. The issue occurs when the secret word has a repeated letter making its length always greater than the guesses length.

Kenneth mentions a solution in one of the later videos and in one of the Teacher's notes.

The key is to collapse the secret work in a unique list of letters. The set function work well here. Changing you `while condition:

# from:
while len(bad_guesses) < 7 and  len(good_guesses) != len(list(secret_word)):

# to: 
while len(bad_guesses) < 7 and  len(good_guesses) != len(set(secret_word)):

Should work.

Changing "set" in those TWO places worked, but why? What does "set" do? Ok so it does this: "collapse the secret word in a unique list of letters". But what does that mean? like alien letters? Collapse? How? Is that literal or just jargon.

@john larson set() is an unordered collection with no duplicate elements.

so if your secret_word = 'avocado', and you run: set(secret_word) it would return {'c', 'a', 'v', 'o', 'd'}

Thanks Chris, that answers my question.

Patrick Kearns
Patrick Kearns
4,669 Points

This still doesn't fix it. Now when I correctly guess a word with a repeated letter, it tells me I lost regardless of the number of strikes.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Hey Patrick, it might be something else in your code. I encourage you to start a new post and include your code. Tag me if you wish.

Nikolaj Bewer
Nikolaj Bewer
1,150 Points

Hey Pratrick, the same occurred when I fixed it. What helped me is changing:

from

if len(good_guesses) == len(list(secret_word)):

to

if len(good_guesses) == len(set(secret_word)):

(it's around line 59).