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) Number Game App Number Game Refinement

Maor Beeri
Maor Beeri
2,154 Points

my code does not run

I get this message error:( except ValueError:
^
IndentationError: unindent does not match any outer indentation level) I still can't see where is my problem - it started to give my headache haha

here is my code I wrote:

import random

def game(): secret_num = random.randint(1,10) guesses = []

while len(guesses) < 5:
    try:
        guess = int(input("Guess a number between 1 and 10: "))

    except ValueError:
        print("{} is not a number".format(guess))

    else:

        if guess == secret_num:

            print("Yay you are correct, my number was {}".format(secret_num))
            break

        elif guess < secret_num:

            print("my number is greater than {}".format(guess)

        else:
            print("my number is less than {}".format(guess))
            guesses.append(guess)
else:
    print("you didn't get it my number was {}
          ".format(secret_num))

    play_again = input("Do you want to play again? Y/n")

    if play_again.lower() != n:
        game()  
    else:
        print(" Good Bye!)

game()

It would be helpful if you posted your code in your question so that we can see exactly what you are submitting. It seems as though your indentation is not correct but without seeing your code it is impossible to offer any further insight.

Steven Parker
Steven Parker
231,269 Points

To be able to check indentation, your code must be properly formatted. Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

1 Answer

Try this:

import random


def game():
    secret_num = random.randint(1, 10)

    guesses = []

    while len(guesses) < 5:
        try:
            guess = input("Guess a number between 1 and 10: ")
            guess = int(guess)
        except ValueError:
            print("{} is not a number".format(guess))
        else:
            guesses.append(guess)
            if guess == secret_num:
                print("Yay you are correct, my number was {}".format(secret_num))
                break
            elif guess < secret_num:
                print("my number is greater than {}".format(guess))
            elif guess > secret_num:
                print("my number is less than {}".format(guess))
                guesses.append(guess)
            else:
                print("you didn't get it my number was {}".format(secret_num))

                play_again = input("Do you want to play again? Y/n")

                if play_again.lower() != n:
                    game()
                else:
                    print("Good Bye!")


game()