Course Content

Python Exercise 3: Solution

Problem Solving Exercise#3:


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.

Have you solved this task? If yes, then it’s time to check your solution. Best of luck.!
Your participation is appreciated. Keep supporting and stay up to date with codewithharry.

.

Code file as described in the video


# no of guesses 9
# print no of guesses left
# No of guesses he took to finish
# game over

n=18
number_of_guesses=1
print("Number of guesses is limited to only 9 times: ")
while (number_of_guesses<=9):
    guess_number = int(input("Guess the number :\n"))
    if guess_number<18:
        print("you enter less number please input greater number.\n")
    elif guess_number>18:
        print("you enter greater number please input smaller number.\n ")
    else:
        print("you won\n")
        print(number_of_guesses,"no.of guesses he took to finish.")
        break
    print(9-number_of_guesses,"no. of guesses left")
    number_of_guesses = number_of_guesses + 1

if(number_of_guesses>9):
    print("Game Over")
  

Comments(4)

zadrain 4 months, 3 weeks ago
n=9
number_of_guesses=1
print("you have only 5 guesses")
while(number_of_guesses<=5):
    guess_number= int(input("guess the number :\n"))
    if guess_number<9:
        print("your guess is too low like your energy.\n ")
    elif guess_number>9:
        print("your guess is too high like my energy,\n")
    else:
        print("you won \n")
        print(number_of_guesses,"no. of guesses you took to finsh")
        break
    print(9 - number_of_guesses,"no. of guesses left")
    number_of_guesses +=1
if(number_of_guesses>9):
    print("game over")
ahmedraza 4 months, 2 weeks ago
nice
Vivek-Mishra 1 month, 2 weeks ago
Thanks, Harry Bhai for Awesome Tutorials |:::::::::::::::::| Full Notes and Source Code :::::::::::::::::::::::::::::::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes
sanam007 4 weeks ago
n = 10
print("GUESS THE NUMBER!  you have 5 times ")
times = 0
while(times<=5):
    inp = int(input())
    times = times+1
    if inp==10:
        print("good job! you win the game. \n")
        print(times, "times you took to finish ")
        break

    if inp>10:
        print(inp, "is big number than n try again! ")
        print(5 - times, "times left \n")
        continue
    if inp<10:
        print(inp,"is small number than n try again! ")
        print(5 - times, "times left \n")
        continue


if (times>5):
    print("GAME OVER! ")

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