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

Lia Gaetano
Lia Gaetano
2,619 Points

Hi! I'm having two runtime errors: 1. game() is not counting/limiting guesses 2. .format doesn't return error value.

Here is code:

import random

def game():
    # generate random number between 1 and 10
    secret_num = random.randint(1, 10)
    guesses = []

    #limit the number of guesses
    while len(guesses) < 3:
######## isn't incrementing?
        try:
            # get number from player
            guess = int(input("Guess a number between 1 and 10: "))
        except ValueError:
            print("{} is not a number!".format(guess))
######## above returns last int input, but not the wrong input i.e. "cat"
        else:    
            # compare guess to secret number
            # print hit/miss
            if guess == secret_num:
                print("You're right!! My number was {}".format(secret_num))
                break
            elif guess > secret_num:
                print("Too high")
                continue
            elif guess < secret_num:
                print("Too low")
                continue
            guesses.append(guess)
    else:
        print("You lost. My number was {}.".format(secret_num))
    play_again = input("Do you want to play again? Y/n ")
    if play_again.lower() != "n":
        game()
    else:
        print("Bye")

game()

1 Answer

Andreas cormack
Andreas cormack
Python Web Development Techdegree Graduate 31,266 Points

Hi Lia

Remove continue from your while loop and it should work. The if statement checking if the entered number is to high or low is causing the loop to start at the very beginning every time, this intern causes the list guesses not to get appended with the guess. Also check your indentation for the else statement that deals with the scenario when the list is greater than 2.

import random

def game():
  # generate random number between 1 and 10
  secret_num = random.randint(1, 10)
  guesses = []
#limit the number of guesses
  while len(guesses) < 3:
    ######## isn't incrementing?
    try:
      # get number from player
      guess = int(input("Guess a number between 1 and 10: "))
    except ValueError:
      print("This is not a number!") #the reason you get an error here is because the variable guess 
      #never gets assigned due to the value error. My solution is not to assign a variable but instead point out its not a 
      # a number. This is a bug 
######## above returns last int input, but not the wrong input i.e. "cat"
    else:    
      # compare guess to secret number
      # print hit/miss
      if guess == secret_num:
        print("You're right!! My number was {}".format(secret_num))
        break
      elif guess > secret_num:
        print("Too high")
      elif guess < secret_num:
        print("Too low")
      guesses.append(guess)
  else:
    print("You lost. My number was {}.".format(secret_num))
    play_again = input("Do you want to play again? Y/n ")
    if play_again.lower() != "n":
      game()
    else:
      print("Bye")

game()

hope that helps