Python Python Basics (2015) Number Game App Number Game Refinement

Abdullah Jassim
Abdullah Jassim
4,551 Points

getting a syntax error.

import random




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

  while len(guesses) < 5:
    try:
      guess = int(input("Can you guess the number between 1 and 10? "))
    except ValueError:
      print("{} is not a number".format(guess))
    else:  

        if guess == random_number:
          print("You got it, my number was {}".format(random_number))
          break

        elif guess < random_number:
          print("Number is too low. Guess again")

        else guess > random_number:
          print("Number is too high. Guess again")
        guesses.append(guess)        

  else:
    print("Your didnt get it! The number was {}".format(random_number)) 
  play_again = input("Would you like to play again? Type Y/n" )
  if play_again.lower() != 'n':
    game()
  else:
    print("Bye")

game ()

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,657 Points

Hi there,

An else clause can't take a condition. It just executes.

if char == 'a':
  print("It's an a")
else:
  print("It's not an a")

So, here, it just executes; the test is done within the if. Where that's a false outcome, the else just runs. There's no additional testing. So, change:

        else guess > random_number:
          print("Number is too high. Guess again")
        guesses.append(guess)

to

        else:
          print("Number is too high. Guess again")
        guesses.append(guess)

Let me know how you get on.

Steve.