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

Ezekiel Wootton
Ezekiel Wootton
1,219 Points

Game will not replay

I actually have TWO problems. The first is that when I first make a guess that is an int (let's say it is 5) and then I guess with a string the second time, the 'except' will say the 5 is not an integer between 1 and 10 instead of the string I just used. The other problem is that when I press any key other than 'n', it keeps asking whether I want to continue playing rather than restarting the game. What am I doing wrong?

Here is the code:

import random

create empty list for guesses

guess_count = []

generate a random number between 1 and 10

secret_number = random.randint(1, 10) def game(): while len(guess_count) < 5: try: # get a number guess from a player user_guess = int(input("Choose a number between 1 and 10: ")) except ValueError: print("{} is not an integer between 1 and 10. Try again.".format(user_guess)) else: # compare guess to secret number if user_guess == secret_number: print("CORRECT! The number I was thinking of was {}!".format(secret_number)) break elif user_guess > secret_number: print("My number is lower than {}.".format(user_guess)) elif user_guess < secret_number: print("My number is higher than {}.".format(user_guess)) # print hit/mis guess_count.append(user_guess) else: print("GAME OVER! My number was {}.".format(secret_number)) replay = input("Would you like to play again? Y/n: ") if replay.lower() != 'n': game() else: print("Bye")

game()

Hi Ezekial!

Thanks for engaging in the Treehouse Community! Can you please format this correctly using the markdown sheet? It just helps me to see it more clearly and accurately, to give you the best support!

Thanks!

Regards,

Joseph

thisIsHowYouFormatYourCode = "Use the Markdown Cheatsheet on the bottom of the post box!"
Mykiel Horn
Mykiel Horn
3,973 Points

My code will not replay when I press anything other than 'n'.

import random

# generate a random number between 1 & 10
secret_num = random.randint(1,10)
guesses = []

def start_game():
  while len(guesses) < 3:
    try: 
      # get a guess number from the player
      player_guess = int(input("""
    Guess a number between 1 and 10: """))
    except ValueError:
      print("{} isn't a number!").format(player_guess)
    else:
    # compare guess to secret number
      if player_guess == secret_num:
        print("You got it!! My number was {}!!!!".format(secret_num))
        break
      elif player_guess > secret_num:
        print("""
  To high, try again.""")
      else:
        player_guess < secret_num
        print("""
  To low, try again""")
      guesses.append(player_guess)
  else:
    print("You didn't get my number, it was {}".format(secret_num))
  play_again = input("Do you want to play again?Y/n")
  if play_again.lower() != 'n':
    start_game()
  else:
    print("Bye")

start_game()

1 Answer

Place secret_num = random.randint(1,10) and guesses = [] inside your start_game() function.