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

Abdullah Jassim
Abdullah Jassim
4,551 Points

Whats wrong with while guesses <= chances:

import random
random_number = random.randint(1, 10)

guesses = []
chances = 10

def game ():
  while guesses <= chances:
    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
        else:
          print("Thats not it. Guess again")
        guesses.append(guess)


game ()

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,562 Points

Hi,

Here, guesses is an empty list and chances is an integer. You can't directly compare those as they're not of the same type.

I think you want to compare the length of guesses. Try:

while len(guesses) <= chances:

The len() method returns an integer representing the length of the list. You can compare that to chances.

Let me know how you get on.

Steve.