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

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

anhedonicblonde
anhedonicblonde
3,133 Points

Indentation error on letter game

I keep getting an indentation error on the letter game. I coded along with the video and everything seems properly indented to match Kenneth's script, but for some reason, I keep getting the same error, even after checking and doublechecking the indentation. I tried to post my code but it didn't post correctly, the indents look nothing like they do in my Workspace when attempting to post code here. The error I'm getting is:

File "lettergame.py", line 46
continue
^
IndentationError: unindent does not match any outer indentation level

"continue" is indented just the same as the "print" command that comes above it, and just as I saw it done in the video. But obviously I have not done something right. Is there any guidance anyone can give? I know it's a tough question since there is no code to look at here, but I can't get the indents to display properly, the code gets all mucked up when I try to post it here. I'm so frustrated.

If you can't answer that question, please answer this one: Is it normal to feel like you should just give up when trying to learn Python? I'm starting to feel like maybe my aptitude is severely lacking.

Thanks for reading.

Gavin Ralston
Gavin Ralston
28,770 Points

No, you shouldn't give up. You'll get it. :)

go ahead and paste the code anyway, even if the formatting is a little off. A mod can clean it up and reformat it, and honestly seeing it just how you pasted it might still be good. If they're all equally incorrectly spaced (say, like EIGHT spaces instead of four on every indentation) it might still give some insight because it'll be a uniform indentation style

anhedonicblonde
anhedonicblonde
3,133 Points

Thank you so much for the encouragement and the reply :)

Here is the code:

import random

# make a list of words
words = [
    'apple',
    'banana',
    'orange',
    'coconut',
    'strawberry',
    'lime',
    'grapefruit',
    'lemon',
    'kumquat',
    'blueberry',
    'melon'    
]


while True:
    start = input("Press enter/return to start, or Q to quit")
    if start.lower() == "q":
        break

    # pick a random word
    secret_word = random.choice(words)
    bad_guesses = []
    good_guesses = []

    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
        # draw guessed letters, spaces, and strikes/missed letters
        for letter in secret_word:
            if letter in good_guesses:
                print(letter, end ="")
            else:
                print("_", end="")

        print('')
        print('Strikes: {}/7'.format(len(bad_guesses)))
        print('')

    # take a guess
        guess=input("Guess a letter: ").lower()

        if len(guess) != 1:
                print("You can only guess 1 letter")
            continue 
        elif guess in bad_guesses or guess in good_guesses:
            print("You've already guessed that letter!")
            continue
        elif not guess.isalpha():
            print("You can only guess letters!")
            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 didn't guess it! My secret word was {}".format(secret_word))

        # print when you win or lose

Thanks so much for having a look! I really appreciate it :)

2 Answers

Gavin Ralston
Gavin Ralston
28,770 Points
if len(guess) != 1:
                print("You can only guess 1 letter")
            continue 

## further down:
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

Everything else, even with the crazy indents in the forum markdown, lines up just fine.

The two spots you seem to be having trouble are at line 46 (that "print" statement is indented just a liiiittle more than the continue line) and around line 56, the "good.guesses.append(guess)" line was directly underneath the "if" statement, and needs to be indented so it'll only be run if "guess in secret_word"

If you're having trouble with the workspaces editor, try putting your cursor just to the left of the offending section, hit backspace until you completely back up to the previous line, then hit enter to start a new line all over again, and tab in til it lines up right. Maybe there's a hidden tab character (which looks just like four spaces) that's throwing python for a loop here.

anhedonicblonde
anhedonicblonde
3,133 Points

backspacing and then entering to start a new line did the trick! it caught a few more errors like that, and I fixed them the same way, and now it works!! Yay! Thank you so much!!! :)

Gavin Ralston
Gavin Ralston
28,770 Points

Awesome, glad I could help.

Be sure to check in the lower right-hand corner of the Workspaces editor window and make sure you have the tabs/spaces button set up the way you want it. It should convert tabs to "4 spaces" instead of 2 spaces, or tabs, or whatever else it'll let you do.

That'll help a bunch. It should be "Python Spaces 4" not like "Python Tab 2"

Kevin Faust
Kevin Faust
15,353 Points

well

if len(guess) != 1:
                print("You can only guess 1 letter")
            continue 

should be

if len(guess) != 1:
                print("You can only guess 1 letter")
                continue 
anhedonicblonde
anhedonicblonde
3,133 Points

Thank you for the reply. it looked normal on my screen but it turned out i needed to backspace out the tabs and hit enter for a new line to get the indents to actually be correct. it's fixed now, thanks again for the heads up on the print statement :)