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:
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') |
No comments:
Post a Comment