Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print the number of inputs entered in python?

Hi I was working on a python guessing game where the computer chooses a number and the user tries to guess the number. Now I want to add a feature to count the number of guesses a user made before getting the correct number. I think for that we need to count the number of inputs. What should I add to reach that goal?Here is my code so far:


print("Welcome to the Guessing Game. In this game, I will choose a random number between 1 and 100 and your goal will be to guess the number I choose. I will tell you if the number you guess is higher or lower than my choice. Lets Start.")

a = random.randint(0, 100)

print("I have chosen my number. Lets start !")

b = int(input("Enter guess"))  

while b != a:  
    if a < b:
        print("LOWER")
    if a > b:
        print("HIGHER")
    b = int(input("Enter guess"))

print("You WIN")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You just need to add a counter and increment it every time the user guesses.

print("Welcome to the Guessing Game. In this game, I will choose a random number between 1 and 100 and your goal will be to guess the number I choose. I will tell you if the number you guess is higher or lower than my choice. Lets Start.")

a = random.randint(0, 100)

print("I have chosen my number. Lets start !")

b = int(input("Enter guess"))  

numGuesses = 1
while b != a:  
    if a < b:
        print("LOWER")
    if a > b:
        print("HIGHER")
    b = int(input("Enter guess"))
    numGuesses += 1

print("You WIN")
print(f"It took you {numGuesses} guesses to get it right!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading