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 trialwesley jackson
2,436 PointsHow 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
Alexander Davison
65,469 PointsBetter 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
31,722 PointsLooks 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
2,436 PointsHi 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
Samuel Ferree
31,722 PointsSamuel Ferree
31,722 Pointscan 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>
'''