I am trying to learn python since 2 days now and I am challenging myself with this little python projects from this website: https://www.upgrad.com/blog/python-projects-ideas-topics-beginners/#1_Mad_Libs_Generator
I am at the second game now (number guessing game).
The instruction is as follows:
'''Make a program in which the computer randomly chooses a number between 1 to 10, 1 to 100, or any range. Then give users a hint to guess the number. Every time the user guesses wrong, he gets another clue, and his score gets reduced. The clue can be multiples, divisible, greater or smaller, or a combination of all. You will also need functions to compare the inputted number with the guessed number, to compute the difference between the two, and to check whether an actual number was inputted or not in this python project.'''
import random
print("Welcome to the number game. A random number will be generated and you can guess which one it is.")
print("You got three tries.")
number = random.randint(1, 99)
guess = "" # var to store users guess
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != number and not out_of_guesses:
if guess_count < guess_limit:
guess = int(input("Enter a guess: "))
guess_count += 1
if guess < number:
print("Your guess is too small. Try again.")
elif guess > number:
print("Your guess is too high. Try again.")
else:
out_of_guesses = True
if out_of_guesses:
print("You are out of guesses. You loose!")
else:
print("Your guess is correct. You Won!")
The output looks like this:
Welcome to the number game. A random number will be generated and you can guess which one it is.
You got three tries.
Enter a guess: 78
Your guess is too high. Try again.
Enter a guess: 28
Your guess is too small. Try again.
Enter a guess: 29
**Your guess is too small. Try again.**
You are out of guesses. You loose!
My problem is the line marked in strong. Actually, after the user entered the third try and did not guess the correct answer, I don't want the line "your guess is too ..." to be displayed. However, before the user is out of tries, I do want it to be displayed.
Do you got any hints on how to adjust the code?
Also, I did understand the concept of try and except, but don't really know where exactly to add it to make the game more "smooth" with regard to entering a wrong input type.
Kind Regards
wow... That is a quality time I spent on your site and that is most worth spending. This article is worth reading & quite good information. If any of the Engineering Students are looking for a python mini project I found this site in the internet Takeoff edu group is the company name there site is pyhton mini projects which is providing the best assistances to students regarding the projects
RépondreSupprimer