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 trialbehar
10,799 PointsLetterman game dosent work
Not sure if you can read my ugly programming, but when i launch this program in python it says: IndentationError: expected an indented block(Line 2). Can you guys spot what i did wrong? Heres my code:
import random
random_words = ["surface", "awesome", "keyboard", "mouse", "castle"]
while True:
start = input("Press enter to start, or Q to quit")
if start.lower() == "Q":
break
secret_word = random.choice(random_words)
wrong_guesses = []
right_guesses = []
while len(wrong_guesses) < 10 and len(right_guesses) != len(list(secret_word)):
for letter in secret_word:
if letter in right_guesses:
print(letter, end="")
else:
print("_", end="")
print("")
print("Strikes: {}/7".format(len(wrong_guesses)))
print("")
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("Your can only guess a single letter!")
continue
elif guess in wrong_guesses or guess in right_guesses:
print("You have already guessed that")
elif not guess_isalpha():
print("You can only guess letter!")
continue
if guess in secret_word:
right_guesses.append(guess)
if len(right_guesses) == len(list(secret_word)):
print("You win! The word was {}".format(secret_word))
break
else:
wrong_guesses.append(guess)
else:
print("You didnt guess it! My secret word was {}".format(secret_word))
2 Answers
Juan Hurtado
2,243 PointsYour last else statement is not in the correct indentation place. This is a common error when someone writes too many if / elif / else in their code. A remedy to this would be abstraction.
For starters, all of the if / elif / else that you're using to handle user errors should be abstracted away in a function for you to call. That way everything is not cluttered, and should an error occur, you know where the problems lies.
Christian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 Pointsalso be careful with .lower at the beginning, make sure it's going to equal a lower case