Decode School


Free Certification Courses from

Guessing Game with Clue in Python

"Guessing Game with Insights"
Create a simple guessing game in which the user must guess the secret number in one attempt. If the user correctly guessed the secret number, the system should display "your guess is correct," otherwise it should tell guessed number is higher or lower than the secret number.
Example If secret number is 3 and guessed number is 4 then system should display "Your Guess is higher than secret number".
Hint : Basic if-elif-else condition is enough for this.
Objective : To learn the usage of if-elif-else conditional statement.

Program or Solution

							
#hashed lines are comment lines used to illustrate the program
#import the built in method randint to generate an automatic secret number
from random import randint
#Generate automatic Secret Number
secret_number = randint(1,5)
#Get a guessing number from user
guess_number = int(input("Guess the secret number between 1 and 5:"))
#check guessing number is correct or higher or lower
if secret_number == guess_number:
print("Your Guess is Correct")
elif guess_number < secret_number:
print("Your Guess is lower than secret number {}".format(secret_number))
else:
print("Your Guess is higher than secret number {}".format(secret_number))

Program Explanation

Line 3: Imports the randint function from the package random

Line 5: using randint system generates a secret number

Line 7 : getting a guess from user

line 9 : check whether secret number generated by system and guessing number entered by user are same

line 10 : if same, display "your guess is correct"

line 11 : if not same, check whether  guessing number entered by user is less than secret number generated by system

line 12 : if lower, display your guess is lower than the secret number

line 14 : if not, display your guess is higher than the secret number