Python Exercise 4 --- "Number Guessing Game"

 Python Exercise 4:

"Number Guessing Game"

Q: 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.


Solution:

print("Hii! Welcome,\n", "This a number guessing game, "
"made by Saswata\n", "Let's Start\n")
n=18
number_of_guesses=1
print("Number of guesses is limited to only 7 times: ")
while (number_of_guesses<=7):
guess_number = int(input("Guess the number :\n"))
if guess_number<18:
print("Your number is lesser than the answer.\n")
elif guess_number>18:
print("Your number is greater than the answer.\n ")
else:
print("you won\n")
print("No.of guesses he took to finish.", number_of_guesses,)
break
print("No. of guesses left", 7-number_of_guesses)
number_of_guesses = number_of_guesses + 1

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

Comments

Popular posts from this blog

Python Tutorial 38 --- "Self & __init__() (Constructors)(OOPS 3)"

Python Exercise 7 --- "Snake, Water, Gun"

Python Tutorial 24 --- "Using Python External & Built In Modules"