Course Content

Python Exercise 3 - Guess The Number

In this tutorial, I have given Exercise 3 (Guess The Number). You can check this question and can try to solve it. It uses simple loops and conditional statements. The problem statement is: 

You have to build a "Number Guessing Game," in which a winning number is set to some integer value. The Program should take input from the user, and if the entered number is less than the winning number, a message should display that the number is smaller and vice versa.


Instructions:

1. You are free to use anything we've studied till now.
2. The number of guesses should be limited, i.e (5 or 9).
3. Print the number of guesses left.
4. Print the number of guesses he took to win the game.
5. “Game Over” message should display if the number of guesses becomes equal to 0.
You are advised to participate in solving this problem. This task helps you become a good problem solver and helps you accept the challenge and acquire new skills.

Code file as described in the video

# list1 = [ ["Harry", 1], ["Larry", 2],
#           ["Carry", 6], ["Marie", 250]]
# dict1 = dict(list1)
#
# for item in dict1:
#     print(item)
# for item, lollypop in dict1.items():
#     print(item, "and lolly is ", lollypop)
items = [int, float, "HaERRY", 5,3, 3, 22, 21, 64, 23, 233, 23, 6]

for item in items:
    if str(item).isnumeric() and item>=6:
        print(item)

Comments(18)

Ankit0651 1 year, 1 month ago
Hii
harry 8 months, 1 week ago
Hey ankit
SachinJ 1 year ago
n= 18

while(True):
    a= input("enter the number")
    if int(a)<18:
        print("input is smaller")
        continue
    elif int(a)>18 :
        print("input is bigger")
    else:
        print("correct", int(a) )
        break
mmishra 12 months ago
num = 18
actaulattempt = 5
attempt = actaulattempt
while attempt > 0:
    question = "Enter number to guess " + str(attempt) + " attempt remain:"
    print(question, end=" ")
    inputnum = int(input())
    if inputnum > num :
        print("your guess number is greater then actual  number")
    elif inputnum < num:
        print("your guess number is lesser then actual  number")
    else:
        print("You won, you guess correct number in ",((actaulattempt-attempt)+1)," attempt")
        break
    attempt = attempt - 1;

print("Game over, you not guess correct number")
Robin 8 months, 1 week ago
n = 16
num_of_Guess = 5

for i in range(num_of_Guess):
    num = int(input("Enter your number: "))
    if num == n:
        print("You guessed right its ", n)
        break
    elif num + 2 == n or num - 2 == n or num + 1 == n or num - 1 == n:
        print("You are really close")
    else:
        num_of_Guess = num_of_Guess - 1
        print("Incorrect! try again")
        continue
Manav_Seng 7 months ago
Name: Manav Sengupta

Solution:

# Code with Harry Excercise 3 -- Guess the Number
Number = 56
No_of_Guesses=9
while(True):
    if(No_of_Guesses<1):
        temp2=int(input(("Game Over! Enter 1 to Continue else Enter anything")))
        if(temp2!=1):
            break
        No_of_Guesses=9
    guess=int(input("Enter your guess\n"))
    if(guess != Number):
        No_of_Guesses=No_of_Guesses-1
        temp= guess>Number
        if(temp):
            verdict="Greater"
        else:
            verdict="Lesser"
        print("Your Guess is "+verdict+" than the Number\n")
        continue
    else:
        print("Congratulations! You got it Right!")
        break
print(147*"*"+"\n*"+145*" "+"*\n*"+9*" "+"Thanks for checking out my code, and a ton of thanks to CodewithHarry Channel for all the Quality Content for Free"+22*" "+"*\n*"+145*" "+"*\n"+147*"*") # Gratitude
GaziAbbas 4 months, 1 week ago
# Gues the Number

guesno = 14
chance = 9
isSucess = False

while (chance > 0):
    print("You Have ",chance," Chance to Gues the No")
    userno = int(input("Gues Number: "))
    chance = chance - 1
    if userno == guesno:
        isSucess = True
        break
    else:
        if (userno > guesno):
            gap = userno - guesno
            if gap <= 5:
                print("Thoda Number Kam Karo Bhai")
            else:
                print("Number Kam Karo Bhai")
        else:
            gap = guesno - userno
            if gap <= 5:
                print("Thoda Number Badhav Bhai")
            else:
                print("Number Badhav Bhai")

if isSucess:
    print("You Win This Game")
else:
    print("Game Over")
Susheel 4 months, 1 week ago
#Susheel Sharma
while(1):
    i=int(input("enter ur number"))
    if i>n and guess!=1:
        print("ur number is greater")
        guess=guess-1
        print("ur r left with chance= ",guess)
        continue
    elif i<n and guess!=1:
        print("ur number is lesser ")
        guess=guess-1
        print("ur r left with chance= ", guess)
        continue
    elif i==n:
        print("ur guess in correct")
        break
    else:
        print("ur chances are over")
        break
Vivek-Mishra 3 months ago
Thanks, Harry Bhai for Awesome Tutorials |:::::::::::::::::| Full Notes and Source Code :::::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes
wbaig390 2 months, 1 week ago
I'm facing some problems..can you guide me on how it is done?

print("enter the number from 1 till 10 in just 3 tries")
print("enter 1st number")
n1 = input()
print("enter 2nd number")
n2 = input()
print("enter 3rd number")
n3 = input()

if n1 < 5:
    print("little more")
elif n1 == 5:
    print("Great Job you guessed the number having all the hints")
else :
    print ("a bit lesser")

if n2 < 5 :
    print("Little more")
elif n2 == 5:
    print("Great Job you guessed the number having 2 hints left")
else:
    print("A bit Lesser")

if n3 < 5:
    print("little more")
elif n3 == 5:
    print("Great Job you guessed the number having only one hint left")
else:
    print("A bit more")

wbaig390 2 months ago
from random import randint
number=randint(1,10)
player_name=input("Hello, what's your name?")
number_of_guess=0
print("Okay! "+player_name+ ' I am Guessing number between 1 and 10:')
while number_of_guess<5:
    guess=int(input())
    number_of_guess+=1
    if guess<number:
        print("Your guess is too low")
    if guess>number:
        print("Your guess is too high")
    if guess==number:
        break
if guess==number:
    print("You guess the number in "+str(number_of_guess)+" tries!")
else:
    print("You did not guess the number,The number was "+str(number))
himanshu9 2 months ago
#lets choose a number
print("Welcome To The Game Called Guess The Number ")
our=19
print("you will have only 8 moves and 8 gusses after you cross the limit,, The game will be over ")
delay = 0
while True:
    delay= 1 + delay
    print("Guess The Number:")
    user=int(input())

    if delay == 7:
        print("now you have only two gusses left so think prefectly !!:)")
    elif delay> 9 :
        print("Game Over")
        break
    #lets make the gusses
    elif user == our:
        print("Conratulation You have the gussed the correct number:)!!")
        print(f"you took {delay} chance")
        break
    elif user == 5 and user < 5:
        print("is less!!")
    elif user ==7 :
        print("Is less!!")
    elif user ==10:
        print("Is more!!")
    elif user ==15:
        print("is more you are now closer")
    elif user > our:
        print("You are Ahead of the Number!!")
    elif user == 20 :
        print("You are near to the number!!")
        print("try your best ")
    elif user == 13:
        print("more")
    elif user == 16 :
        print("you are  very close to that number:")
    else:
        continue
shellstar 1 month, 2 weeks ago
items = ("18")
while(True):
    inn = int(input("guess a number,\n you get 5 chances,\nchance-1\n:- "))
    if inn == 18:
        print("Winner,\nNo of Guesses You Took to Finish-1")
        break
    elif inn > 18:
        print("guess smaller number\n")
    elif inn < 18:
        print("guess bigger number\n")

    innn = int(input("try Again,\nchance-2\n:- "))
    if innn == 18:
        print("Winner,\nNo of Guesses You Took to Finish-2")
        break
    elif inn > 18:
        print("guess smaller number\n")
    elif inn < 18:
        print("guess bigger number\n")

    innnn = int(input("try Again,\nchance-3\n:- "))
    if innnn == 18:
        print("Winner,\nNo of Guesses You Took to Finish-3")
        break
    elif inn > 18:
        print("guess smaller number\n")
    elif inn < 18:
        print("guess bigger number\n")

    innnnn = int(input("try Again,\nchance-4\n:- "))
    if innnnn == 18:
        print("Winner,\nNo of Guesses You Took to Finish-4")
        break
    elif inn > 18:
        print("guess smaller number\n")
    elif inn < 18:
        print("guess bigger number\n")

    innnnnn = int(input("try Again,\nLast chance-5\n:- "))
    if innnnnn == 18:
        print("Winner,\nNo of Guesses You Took to Finish-5")
        break
    else:
        print("game over")
        break
thesunil 1 month ago
num = 7
Life_line = 5
life_Line_used = 0
while(True):
    user_num = int(input('>>'))
    if user_num != num and life_Line_used < 5:
        print("please retry again ")
        life_Line_used = life_Line_used + 1
        continue

    elif user_num == num and life_Line_used < 5:
        print('You won the game')
        print("You life line used : ", life_Line_used)

    elif life_Line_used == 5:
        print('........Game Over .......')
        print("You Lose the game")
        print("You life line used :  ", life_Line_used)
    break
thesunil 1 month ago
num = 7
Life_line = 5
life_Line_used = 0
while(True):
    user_num = int(input('>>'))
    if user_num != num and life_Line_used < Life_line:
        if user_num > num:
            print("you guess greater number")
        elif user_num < num:
            print("you guess smaller number")
        print("please retry again ")

        life_Line_used = life_Line_used + 1
        continue

    elif user_num == num and life_Line_used < Life_line:
        print('You won the game')
        print("You life line used : ", life_Line_used)

    elif life_Line_used == Life_line:
        print('........Game Over .......')
        print("You Loose the game")
        print("You life line used :  ", life_Line_used)
    break
rahul4837 2 weeks ago

Please Help Harry Bhai
Isme Loop hee nahi chal raha hai jab me wrong guess dal raha hu
num = 17
chances_get = 5
chances_left = 0

while True:
    userinp = int(input("Enter the number you guessed"))
    if userinp > num and chances_left < chances_get:
      print("Your Guessed Number is More than The Real Number You have to Guess")
    elif userinp < num:
        print("Yor must increase the value you are guessing")
    elif userinp + 2 == num or userinp + 3 == num or userinp - 2 == num or userinp - 3 == num or userinp - 4 == num:
        print("You are Too close in guessing")

        chances_left = chances_left + 1
        continue

    elif userinp == num and chances_left < chances_get:
        print('You won the game')
        print("You life line used : ", chances_left)

    elif chances_left == chances_get:
        print("***Over***")
        print("You have used all your attempts and not you don't have rights to attempt more \n your attempts are ",+ chances_left)
    break
hp4140p 3 days, 22 hours ago
I have really enjoyed your videos... Here's my program
Please visit this link:-
https://harshitpython4140p.blogspot.com/2020/09/guess-number.html
ape 3 days, 21 hours ago
print("lets start the game\n","enter your digit")
for i in range(9):
    digit = int(input())
    d=i+1
    if d==9:
        print("Game Over")
    elif digit==18:
        print("you won\n","count left=",8-i)
        break
    elif digit>18:
        print("enter lesser value\n","count left=",8-i)
    else:
        print("enter greater value\n","count left=",8-i)
    i+1

Resources

No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here