Number Guessing Game

Creating a number guessing game is a great first game to create because it is fun to play and not too difficult to create. So let’s get started.

  • The first thing we have to do is import random so that the number that the user has to guess changes every time the code runs.
  • Now we are going to set guessesTaken equal to 0 because the user hasn’t taken any guesses.
  • Next we are going to print Hello what is your name to the user
  • myName = input() is how we are going to store the users name
  • number = random.randit(1,20) this is how we are going to create the number that has to be guessed by the user.
  • Now you have to explain to the user how to play the game print(‘well, ‘ +myName + ‘, I am thinking of a number between 1 and 20; you have 5 guesses to figure it out.’)
  • while guessesTaken <6: is telling the computer everything inside of this loop is what happens between guess 1 and 5
  • print(‘Take a guess.’)
  • guess = input() is a function for keeping the number that the user inputs
  • guess = int(guess)
  • guessesTaken = guessesTaken +1 – is telling the computer that the user has submitted another guess.
  • if guess < number:
  • print(‘your guess is too low)
  • if guess > number:
  • print(‘Your guess is too high)
  • if guess == number:
  • break – we have to break if the number is correct because if not the loop will continue.
  • if guess == number:
  • guessesTaken = str(guessTaken)
  • print(‘Good job, ‘ + myName (The users name) + ‘! you guessed my number in ‘ + guessesTaken + ‘guesses!’)
  • if guess != (meaning not equal) number:
  • number = str(number)
  • print(‘Nope. The number I was taking of was ‘ + number).

This is what your output code should look like once it’s run.

This is what the entire code looks like –

CONGRATS! You finished your first game.

Leave a Reply

Your email address will not be published. Required fields are marked *