Guess the number game in python
Python mini project - Guess the number game
Everyone will be looking for a mini project after
learning the basic programming language concepts. Here you will be able to find
a very basic mini project using python.
Explanation
:
Ask the user to enter a number between 1 and 10. At
the same time generate a random number between 1 and 10. If the number entered
by the user matches the random number generated increase the score of the user.
Alternatively, if the number guessed by the user doesn't match the random
number generated, proceed with the next attempt. The user will be given a total
of five numbers to guess. Each time the use r guesses right, the score will be
increased and the final score will be calculated for 5 marks.
Constraints
:
The user is given five numbers to guess.
Total score is five.
For each new attempt a new random number is
generated.
Final score will be calculated for five.
For example,
Let the random number be 6 and if the user entered 3
then no score is given for this attempt and the user is required to continue
with the remaining attempts.
And if, the random number is 7 and if the user too enters the same number 7, then the score is increased.
We are using random module of python here to
generate random numbers. Random. choice() function is used here. The choice
method returns a randomly generated number from a specified sequence.
Procedure
:
1. 1. Start.
2. 2. Import
random module for generating random numbers.
3. 3. Create
a number list with numbers ranging from 1 to 10.
4. 4. Initialize
score as 0.
5. 5. Initialize
number of attempts.
6. 6. Loop
( 0 to 5)
=> Generate
random number using random module.
=> Get
the number as input from the user.
=> If
(number = random number) => increase the score
=> Otherwise
continue
=> Decrement
the number of attempts.
7. 7. Print
the score of the user.
8. 8. End.
Code
:
# library that we use in order to choose
# on random number from a list of numbers
import random
#get the name of the user
name = str(input("Enter your name : "))
#welcome message for user
print ("Good luck ! ", name)
#print the number of chance to guess
print("You have 5 chance to guess")
print("Enter any number from 1 to 10...")
#create a list of numbers ranging from 1 to 10
num_list = []
for i in range(1, 11) :
num_list.append(i)
#initialize score
score = 0
#initialize the number of guesses
guess = 5
i=0
while(i<5):
#
Function will choose one random
#
number from this list of numbers
num
= random.choice(num_list)
#ask
the user to enter a number between 1 and 10.
number
= int(input("guess : "))
#check
if the number entered by the user is equal to the random number generated
#if
the user guessed the number correctly increase the score
if(number
== num):
print("Hurrah!
your guess is right ")
#increase
the score
score
+= 1
else
:
print("Oh!
you guessed wrong")
#decrement
the number of guesses
guess
= guess - 1
print("You
have ", guess, "left")
i+=1
#print the score
print("Score : ", score)
Output :
Guess the number - Output |
Comments
Post a Comment