Click here to Skip to main content
15,745,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I count only as one try if they input the same number multiple times consecutively in a guessing game that keeps track of the number of attempts in Python?

What I have tried:

<pre>import random

repeat = 'y'
while repeat.lower() == 'y':
    n = int(input("Enter value of n(1 - 100) "))
    n2 =int(input("Enter value of n2(1 - 100) "))
    a= random.randrange(n, n2)
    guess = int(input("Guess the hidden number"))

    while guess !=a:
        print('entry number:',i)
        if guess<a:
            print("you need to guess higher. Try again")
            guess = int(input("Guess the hidden number"))
        if guess>a:
            print("you need to guess lower. Try again")
            guess = int(input("Guess the hidden number"))
        if guess == a:
            print('it took you:',i,'tries to guess the hidden number')
            print('Congratulations, the hidden number is',a)
    repeat = input("\nDo you want to try again Y/N?  \n>>> ")
while repeat.lower() == 'n': 
    print('thank you')
Updated 11-Dec-21 19:52pm

1 solution

Add a variable outside the loop and initialize it to 0.
Inside the loop, check the count.
If it's bigger than the limit, tell them they failed, and exit the loop.
Otherwise, add one to it, and let them try again.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900