JavaOnExperiance Java Python Deep Learning tKinter Interview

Number guess games in python

We are going to make a "Guess the Number" game. In this game, the computer will think of a random number from 1 to 20, and ask you to guess the number. You only get six guesses, but the computer will tell you if your guess is too high or too low. If you guess the number within six tries, you win.

Number guess games :

# This is a guess the number game. import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
   print('Take a guess.') # There are four spaces in front of print.
   guess = input()
   guess = int(guess)

   guessesTaken = guessesTaken + 1

   if guess < number:
      print('Your guess is too low.') # There are eight spaces in front of print.

   if guess > number:
     print('Your guess is too high.')

   if guess == number:
       break

   if guess == number:
     guessesTaken = str(guessesTaken)
     print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

   if guess != number:
      number = str(number)
      print('Nope. The number I was thinking of was ' + number)

output :

Hello! What is your name?
Albert
Well, Albert, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too high.
Take a guess.
2
Your guess is too low.
Take a guess.
4
Good job, Albert! You guessed my number in 3
guesses!