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
Mateo Rial
3,694 PointsI don´t understand why it doesnt run the same way in the video ...if I guess all the letters the program continue asking
import random
words=[
"apple",
"banana",
"orange",
"coconut",
"strawberry",
"lime",
"grapefruit",
"lemon",
"kumquat",
"blueberry",
"melon"
]
while True:
start=input("Press enter to start or Q to quit")
if start.lower()=="q":
break
secw=random.choice(words)
badg=[]
goodg=[]
while len(badg) < 7 and len(goodg)!=len(list(secw)):
for let in secw:
if let in goodg:
print(let,end="")
else:
print("_",end="")
print("")
print("strikes: {}/7".format(len(badg)))
print("")
guess=input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess a single letter!")
continue
elif guess in badg or guess in goodg:
print("You´ve already guess that letter!")
continue
elif not guess.isalpha():
print("You can only guess letters!: ")
continue
if guess in secw:
goodg.append(guess)
if len(goodg) == len(list(secw)):
print("You win! The word was {}".format(secw))
break
else:
badg.append(guess)
else:
print("You didnt guess it! the secret word was {}".format(secw))
1 Answer
Sean Manners
14,816 PointsI would run the program a few times, at least until you get a word that doesn't have a duplicate letter in it (like 'lemon'). One thing I noticed on this challenge is it may keep running if it has duplicate letters, as it considers you complete if the number of guesses is equal to the secret word. The problem with that is that if there are two or more of the same letter, that condition can never be true because multiple 'e' s' will not count as you guessing three of them, but rather one guess that fills three spots.