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


Guessing Game challenge

I decided to write my code before looking at Ken's. However, when running my script I keep getting an error message that says,"indentation error: expected indent. . . ". so here's the code again (I just realized I was using the wrong thing).

def validate_guess():
if plyr_guess != int:
    print("Whoops! You didn't enter a number!")
    plyr_guess = input("Please enter a number between 1-10.")
elif plyr_guess > 10:
    print("Whoops! You're number should be between 1-10.")
    plyr_guess = input("Please enter a number between 1-10.")

def count_guess():
while True:
    count < 4:
    if plyr_guess != target_num:
    elif plyr_guess < target_num:
        print("Nope! Guess higher!")
        count += 1
    elif plyr_guess > target_num:
        print("Nope! Guess lower!")
        count += 1
    print("Congratulations! You won in", count, " tries!")

import random
target_num = random.randint(1,10)
total = 0
count = 0
#Explain the rules of game
print("Hmmmm. . . . I'm thinking of a number between 1 and 10. Can you guess it? I'll give you 3 tries.)"
plyr_guess = input("Please enter a number between 1-10.")
#Read guess and validate that plyr_guess is an integer.


Forget it, I'll just go online and get it. I can get the indents to work there. Thanks anyway.

2 Answers

Joshua Ferdaszewski
Joshua Ferdaszewski
12,716 Points

Hi David, you may have already figured this out, but I thought I would offer help if you still need it. All of the code that is part of the validate_guess() and count_guess() function definitions needs to be indented. That is probably the source of your error. A few other things to note:

  • At the start of your while block you have a line of code: count < 4: with an if statement following. This line will probably throw an error as it is expecting a block of code after the : and the way it is written now, this line just evaluates the expression then does nothing with it.
  • The if plyr_guess != target_num: line does not have any code in it's block so it will cause an indentation error as well.
  • One final style thing, it is best to include all imports at the very top of your python script. Currently the random library is getting imported in the middle.

Keep up the great work and good luck learning python!

thanks Joshua! That helped out alot.