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

Letter game

Hi guys, I follow the video and sometimes the guess will be correct if the secret word does not have duplicate letter. For example lemon. But the word has duplicate letter does not work, like kiwi, which has two 'i', and banana which has many 'a' and 'n'. I try to add 'count' not use len(good guess) but does not work

import random
words=[
    'apple',
    'orange',
    'peach',
    'lemon',
    'kiwi',
    'pineapple',
    'strawberry',
    'blueberry']
while True:
    start= input("Please enter/return to start, or enter Q to quit: " )
    if start.lower()=='q':
        break
    secret_word= random.choice(words)
    good_guess=[]
    bad_guess=[]    

    while len(bad_guess)<7 and len(good_guess)!=len(list(secret_word)):
        for letter in secret_word:
            if letter in good_guess:
                print(letter,end='')
            else:
                print('_',end='')    


        print('')        
        print('Strike: {}/7'.format(len(bad_guess))) 
        print('')

        guess=input("Guess a letter: ").lower()
        if len(guess)!=1:
            print("You can only enter 1 letter")
            continue    
        elif guess in good_guess or guess in bad_guess:
            print("You already guess this letter")
            continue
        elif not guess.isalpha():
            print("This is not a letter")
            continue    

        if guess in secret_word:
            good_guess.append(guess)
            if len(good_guess)==len(list(secret_word)):
                print("You win the word is {}".format(secret_word))
                break  
        else:
            bad_guess.append(guess)
    else:
        print("Sorry, you lose")

2 Answers

Steven Parker
Steven Parker
243,199 Points

The strategy currently implemented to detect a win only works for words with unique letters, as you've noticed.

As you continue on in the course, another method will be introduced that will work on any word.

Thank you for you help