Site icon Console Flare Blog

Python Game : How To Create A Numb3r Guessing Game In Python

So Today we will be creating an awesome Guess The Number Python Game. This is a python project for beginners and will use the basics of python.

Game Rules :

Let’s Start

you are reading python game : guess the number game in python

Generating a Random Number

To generate a Random Number between 1 to 100 , We will be using a module named ‘random’ .


import random as r

random_number = r.randrange(1,100)

print(random_number)

Guessing the Number

Now we will ask user to guess the number , simply by user input.

guess_number = input('Guess the Number : ')

Checking the Number

Now we will compare Guess_number with Random_number by using if-elif statement.

if(random_number == guess_number):
  print('You Won')
elif(random_number > guess_number):
  print('Hint : Choose a Higher Number')
else:
  print('Hint : Choose a Lower Number')

Adding Loop so that Game Doesn’t end in First Guess

while(True):
  guess_number = int(input('Guess the Number : '))

  if(random_number == guess_number):
    print('You Won')
  elif(random_number > guess_number):
    print('Hint : Choose a Higher Number')
  else:
    print('Hint : Choose a Lower Number')

Game Doesn’t end if i win ?

Now game is working fine , though even after I have guessed the number , it keeps asking for more . It’s because I have used infinite loop . All I need to do is when I have guessed the number , I will use break to get out of the loop.

you are reading python game : guess the number game in python

while(True):
  guess_number = int(input('Guess the Number : '))

  if(random_number == guess_number):
    print('You Won')
    break
  elif(random_number > guess_number):
    print('Hint : Choose a Higher Number')
  else:
    print('Hint : Choose a Lower Number')

Adding Maximum Attempts in Game

Let’s add that user must guess in 10 attempts and if he is not able to guess in 10 attempts , he will lose as he ran out of his chances. For this we will use else of while loop.

you are reading python game : guess the number game in python

chances = 1
while(chances<=10):
  print(f'YOU HAVE {10-chances} LEFT')
  guess_number = int(input('Guess the Number : '))

  if(random_number == guess_number):
    print('You Won')
    break
  elif(random_number > guess_number):
    print('Hint : Choose a Higher Number')
  else:
    print('Hint : Choose a Lower Number')
  chances = chances + 1
else:
  print('Oops!! You lost , ran out of your chances.')

Adding Score

Now adding score is not a big deal, all you need to do is to create a formulae that scores big when user has guessed in less number of attempts.

you are reading python game : guess the number game in python

score = 110
chances = 1
while(chances<=10):
  print(f'YOU HAVE {10-chances} CHANCES LEFT')
  guess_number = int(input('Guess the Number : '))

  if(random_number == guess_number):
    print(f'You Won. Score : {score-10*chances} ')
    break
  elif(random_number > guess_number):
    print('Hint : Choose a Higher Number')
  else:
    print('Hint : Choose a Lower Number')
  chances = chances + 1
else:
  print('Oops!! You lost , ran out of your chances.')

Whole Code

import random as r

random_number = r.randrange(1,100) #Generating Random Number

print(random_number)

score = 110   
chances = 1
while(chances<=10):
  print(f'YOU HAVE {10-chances} CHANCES LEFT')
  guess_number = int(input('Guess the Number : '))

  if(random_number == guess_number):
    print(f'You Won. Score : {score-10*chances} ')
    break
  elif(random_number > guess_number):
    print('Hint : Choose a Higher Number')
  else:
    print('Hint : Choose a Lower Number')
  chances = chances + 1
else:
  print('Oops!! You lost , ran out of your chances.')

Conclusion

This Python Project used the basics of python like conditional statements and loops. You can make many changes in this game and make it more interesting. For anyone who is learning python , this little project will help him a lot to understand the basics of the python. To learn more about python , you can surf through our ConsoleFlare . To learn Python and become master in it, You can opt to our Course / Live Sessions . Check out our Courses here.

Show your love on Instagram : ConsoleFlare

you were reading python game : guess the number game in python

Exit mobile version