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
Christopher Wommack
3,983 PointsHelp I don't know what I am doing wrong
I am getting a invalid syntax on line 8 with the little pointer under the t on except. Any help is appreciated also if I may ask what makes the computer ask for another input after you get the wrong answer? I have had to look up just about everything in the community is that normal or am I struggling more than I should?
Christopher Wommack
3,983 Pointsimport random
def game():
guesses = []
num = random.randint(1, 10)
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 == num:
print("you guessed it correctly. my number was {}".format(num)
elif: guess > num:
print("My number is lower than yours".format(guess))
elif: guess < num:
print("My number is higher than yours".format(guess))
else:
print("That's not it")
guesses.append(guessees)
play_again = input("Do you want to play again? Y/n")
if play_again.lower() !='n':
game()
else:
print("Bye!")
game()
Alexander Davison
65,469 PointsUmmm don't use the ' character, try using the ` (right below the Esc key on your keyboard) character instead.
Your code is still un-readable to us :)
4 Answers
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Christoper
There are alot of errors in your code. See below
import random
def game():
guesses = []
num = random.randint(1, 10)
while len(guesses) < 5:
try:
guess =int(input("guess a number between 1 and 10") # have not closed the Int function properly
except ValueError:
print("{} is not a number".format(guess))
else:
if guess == num:
print("you guessed it correctly. my number was {}".format(num) # have not closed print properly
elif: guess > num: # this should be elif guess > num:
print("My number is lower than yours".format(guess))
elif: guess < num: # this should be elif guess < num:
print("My number is higher than yours".format(guess))
else: # indent this block
print("That's not it")
guesses.append(guessees)# this should be guesses.append(guess)
play_again = input("Do you want to play again? Y/n")
if play_again.lower() !='n':
game()
else:
print("Bye!")
game()
Christopher Wommack
3,983 PointsThank you for your help I corrected those errors now it is showing the if guess == num: TabError: inconsistent use of tabs and spaces in indentation and it's pointing at the colon
alex novickis
34,894 Points"inconsistent use of tabs and spaces in indentation" - this means you have tabs and spaces for indent pick one type of indent character and stick to it, don't mix them
since they both look the same it's a bit difficult to find them after you put them in some editors will help you out with this - for example, in sublime you can go to View->Indentation->Convert indentation to spaces
Otherwise you will have to go line by line and see what you did by deleting the indent and putting it back with spaces
Christopher Wommack
3,983 PointsI am trying to get the game to let me make multiple guesses and tell me when I have won I am sure I have something wrong or am missing something
import random
def game():
''' return to game().'''
def guess_again():
guess_again = int(input("guess again: "))
''' return to guess_again().'''
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 == num:
print"You guessed it correctly. My number was {}".format(num)
break
elif guess > num:
print"My number is lower than yours {}".format(guess)
guess_again()
return
else:
print"My number is higher than yours {}".format(guess)
guess_again()
return
guesses.append(guesses)
else:
play_again = input("Do you want to play again? Y/n")
if play_again.lower() != 'n':
game()
else:
print"Bye!"
break
game()
guess_again()
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Christopher
Dont get what you mean by multiple guesses. The while loop already lets you guess 5 times. I amended your code a bit see below
import random
guesses = []
def game():
num = random.randint(1, 10)
while len(guesses) < 5:
try:
guess = int(input("guess a number between 1 and 10: "))
except ValueError:
print("You did not type a number ")
game()
else:
if guess == num:
print("You guessed it correctly. . My number was {}".format(num))
break
elif guess > num:
print("My number is lower than yours.. My number was {}".format(num))
else:
print("My number is higher than yours. My number was {}".format(num))
guesses.append(guesses)
else:
print("Game Over !!")
play_again = input("Do you want to play again? Y/n")
if play_again.lower() != 'n':
game()
else:
print("Bye!")
game()
alex novickis
34,894 Pointsalex novickis
34,894 Pointsthis is normal.
Take a read through the "Markdown Cheatsheet" and paste in your code, and a log of the exact error - so we can see what's going on
Since it says syntax error, it means you didn't format something the way python wants, missing semicolon, or wrong indentation
http://inventwithpython.com/blog/2012/07/09/16-common-python-runtime-errors/