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
Matt Brown
7,782 PointsNumber Guessing Game - Getting Error: "TabError: inconsistent use of tabs and spaces in indentation on line 9.
import random
def game(): # generate a random number between 1 and 10 secret_num = random.randint(1,10) guesses = []
# Limit the number of guesses
while len(guesses) < 5:
try:
# get a number guess from the player
guess = int(input('Guess a number between 1 and 10: '))
# Catch when someone submits a non-integer
except ValueError:
print("{} Isn't a number!".format(guess))
else:
# compare guess to secret number
if guess == secret_num:
# print hit/miss
print('You got it! My number was {}'.format(secret_num))
break
# Print 'too low' or 'too high' messages for bad guesses
elif guess > secret_num:
print("Too high!")
else:
print("Too low!")
guesses.append(guess)
else:
print("GAME OVER, my number was {}".format(secret_num))
# Let people play again
play_again = input('Do you want to play again? y/n')
if play_again.lower() == 'y':
game()
else:
print("Deuces!")
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! Python depends a lot on white space. And you may not use both tabs and spaces inside the same file for indentation. You need to stick to one. Otherwise, you will get the tab indentation error. So if you started this project, and used tab to for your first indentation, you need to do the same thing for every indentation. Likewise for spaces. Hope this helps!