Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

wesley jackson
wesley jackson
2,436 Points

How does entering the letter 'q', quit this program?

Having noticed that 'q' did not quit my game I have been looking for the code which I may have missed but I have been unsuccessful in doing so. I would welcome any pointers for any oversight which I may have made. Much appreciated.

import random
# make a list of words
words= ['apple', 'bananna', 'orange','coconut','strawberry','lime'
    , 'grapefruit','lemon','kumquat','bluberry','melon'
    ]
while True:
    start = input("press enter/ return to start or enter q to quit")
    # pick a random word
    secret_word = random.choice(words)
    bad_guess = []
    good_guess = []

    while len(bad_guess) < 7 and len(good_guess) != len(list(secret_word)):

        # draw spaces
        for letter in secret_word:
            if letter in good_guess:
                print(letter, end='')
            else:
                print('_', end='')

        print('')
        print('Strikes: {}/7'.format(len(bad_guess)))
        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_guess or guess in good_guess:
            print("You already guessed that letter")
            continue
        elif not guess.isalpha():
            print("You can only get letters")
            continue


        if guess in secret_word:
            good_guess.append(guess)
            if len(good_guess) == len(list(secret_word)):
                print("You win! The word was {}".format(secret_word))
                break
        else:
            bad_guess.append(guess)
    else:
        print("You didn't get it! My secret word was {}".format(secret_word))
            # draw guessed letters and strike
            # print out win/lose     

Btw, thank you for the 'back ticks' tip. you have made my life a less challenging in posting questions. Also this is the code video tutorial

Samuel Ferree
Samuel Ferree
31,721 Points

can you post your code?

for formatting, paste it between three backticks, specifying python for the language like so.

I've used single quotes, but you'll want to use the backtick to the left of the 1 key

'''python

<paste your code here>

'''

Better example of code formatting (I'm using backticks in this example):

```python

<your code goes here>

```

Output when you post the code:

<your code goes here>

2 Answers

Samuel Ferree
Samuel Ferree
31,721 Points

Looks like you're not checking if the user entered 'q' to quit. You collect some input, stored in the variable start but then do nothing with it. If you add an if statement and break after the input, it should allow the user to quit the game by entering q

while True:
  start = input("press enter/return to start or enter q to quit")
  if start == "q": #or start.lower()[0] if you wanted to be more permissive
    break
  # etc. 
wesley jackson
wesley jackson
2,436 Points

Hi Samuel,

Yes, that was my point as this was straight from the tutorial and it seems that the instructor forgot as this criteria was not tested. Just wanted to confirm that I was not overlooking anything.

Cheers and thank you for the fix