Friday, March 15, 2019

The Guessing Game!

Here is a simple and fun, number guessing game. This game illustrates the use of the function randint() from the random library.

A little about the randint() funtion:
The randint() function takes 2 numbers as arguments and returns a random number between the 2 numbers (inclusive of the lower and upper limit numbers). The randint() function is used as shown below,

import random
c = random.randint(50,250)
print(c)

Here, a random number between 50 and 250 will be stored in c. Both 50 and 250 are also included in the range.

Code for the Guessing Game:

import random
secret = random.randint(1,100)
print('Guessing Game')
print('Guess the secret number between 1 and 100')
num = int(input('Please enter your guess:'));
while(num != secret):
if num > secret:
print('Your guessed number is too high')
else:
print('Your guessed number is too low')
num = int(input('Please enter your guess again:'))
print('You guessed it right!')
view raw GuessingGame.py hosted with ❤ by GitHub

No comments: