Packaging TeX Live For MSYS2

Random number game in python

Game Random Number

Yeah! It's been a long wish for everybody I believe has wished to create a game for a desktop or a mobile phone. It's a bit easy if you programming and have good logic. In the pace of using Python a programming language, I am going to help you out in creating a new game. This game is purely for beginner's who are learning python. This is a game is to generate a random number and store it. Ask the user for the number to be guessed and give hints. By this way, the user can guess the number.

Algorithm


  1. Create a random number using the random module in python.
  2. Save number to a variable.
  3. Ask for input from the user. Check whether it is equal to number saved. If it is equal check for playing again.
  4. Elseask continuously the number by giving hints about whether the number is higher or lower.
  5. That's Not so hard.


Coding


1
 2
 3
 4
 5
 6
 7
 8
 9
10
import random
a=random.randint(1,100)
b=int(input('Guess a number between 1 and 100 inclusive'))
while b!=a:
    if a<b:
        print('The number is less')
    else:
        print('The number is more')
    b=int(input('Sorry, enter again'))
print('Congradulation, You have Found The number to be',a)


Explanation

Here, I used the random module which is default with, Python installation. Using the randint() function in that helps us to generate a random number. This asking user is bit easy one. I just used a while loop to check and it worked!
Hope this one helps.

Comments

Post a Comment