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

Ikechukwu Arum
Ikechukwu Arum
5,337 Points

For some reason, my code does not store the letter "p" and does not terminate once i get it right. Please help

import random

fruits = ["Orange", "Peach", "Apple", "Banana",
"Pineapple","Pear","Cantalope","Mango","Plum","Melon","strawberry"]
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(fruits)
    bad_guesses = []
    good_guesses = []
    print(secret_word)
    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
        #draw spaces
        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! ")
            continue
        elif not guess.isalpha():
            print("you can only guess alphabets!")
            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! the fruit type was {}".format(secret_word))

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Ikechukwu,

The problem is that you have words with capital letters, but you're lower()ing any input guesses. So it's impossible to win if the word starts with a capital letter!

You need to either check the lowercased letter against the lowercased word, or make sure all the words are all lowercase.

Hope that makes sense!

Cheers :beers:

-Greg

Ikechukwu Arum
Ikechukwu Arum
5,337 Points

thanks for the reply. i tried changing it and it still does not take the first take all the letters