15,745,574 members
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:

Python
```<pre>import random

repeat = 'y'
i=1
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')
break```
Posted
Updated 11-Dec-21 19:52pm

## Solution 1

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.