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 trialDiwakar Singh
Courses Plus Student 1,621 Pointswhat is wrong with my code
import random
words=['apples','banana','grapes','orange','lemon','melon','strawberry']
while True:
start=input("Press enter/return to play or q to quit ")
if start.lower()=='q':
break
secret_word=random.choice(words)
good_guesses=[]
bad_guesses=[]
while len(bad_guesses)<7 and len(good_guesses)!=len(list(secret_word)):
for letter in secret_word :
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
print("strikes : {}/7".format(len(bad_guesses))
guess=input("guess a letter :").lower()
if len(guess)!=1;
print("you can guess only single letters ")
continue
elif guess in bad_guesses or guess in good_guesses:
print("you already guessd that letter ")
continue
elif not guess.isaplha():
print("you can only guess alhabets ")
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 lose my word was {} ".format(secret_word))
[MOD: added ```python formatting -cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsThe flow of your code is basically correct. There are syntax errors and typos that need to be fixed. And one logic error. The best way to seek help with code errors is to also post the error you see when the code fails.
- first error
File "<string>", line 21
guess=input("guess a letter :").lower()
^
SyntaxError: invalid syntax
A syntax error is sometimes caused by the preceding line:
print("strikes : {}/7".format(len(bad_guesses)) # missing paren
- second error
File "<string>", line 22
if len(guess)!=1; # Need colon not semicolon
^
SyntaxError: invalid syntax
- third error
Press enter/return to play or q to quit
_____
strikes : 0/7
guess a letter :a
Traceback (most recent call last):
File "<string>", line 28, in <module>
AttributeError: 'str' object has no attribute 'isaplha'
elif not guess.isaplha(): # typo should be isalpha
- finally, when comparing the length of
good_guesses
to the length ofsecret_word
, if thesecret_word
has repeated letters then the number of good guesses will never match the length of the secret word. You can useset
instead oflist
to get a true comparison.
# here
while len(bad_guesses)<7 and len(good_guesses)!=len(set(secret_word)):
# and here
if len(good_guesses)==len(set(secret_word)):
Post back if you need more help. Good luck!!!
Diwakar Singh
Courses Plus Student 1,621 PointsBut what the set is doing??
Chris Freeman
Treehouse Moderator 68,441 PointsA set does not allow duplicate items. This automatically reduces the secret word down to its unique letters. See more in the set docs
Diwakar Singh
Courses Plus Student 1,621 PointsDiwakar Singh
Courses Plus Student 1,621 Pointsi don't understand the third error
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsIt's a typo: should be
isalpha
not "isaplha"Diwakar Singh
Courses Plus Student 1,621 PointsDiwakar Singh
Courses Plus Student 1,621 Pointsthanks