Decode School


Free Certification Courses from

Simple Guessing Game in Python

"Simple Guessing Game"
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 display the message "your guess is wrong" and the guessed number.
Hint : Basic if-else condition is enough for this.
Objective : To learn the usage of if-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 not
if secret_number == guess_number:
print("Your Guess is Correct")
else:
print("Your Guess is Wrong, the secret number is {}".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 12 : if not same, display "your guess is wrong"