'How to continue with number of guesses after ValueError in guessing game?

I'm going through the Automate the Boring Stuff with Python videos on youtube. I finished the one about random number guessing game and am now adding some stuff to it to make it more complete.

I added some code that makes it so that if you don't enter an integer (which creates a ValueError) the program tells you that you didn't enter an integer. However, the point of the game is that you only get 6 guesses, but when the error comes up it, it resets the number of guesses.

How can I make it so that the error doesn't reset the number of cases?

Here's the code:

# This is a guess the number game.
import random

print ('Hello, what is your name?')
name = input ()

name = name.strip()

print ('Well, ' + name + ' I am thinking of a number between 1 and 20.')
secretNumber = random.randint (1, 20)

print ('DEBUG: Secret number is ' + str(secretNumber))

while True: 
    try:
        for guessesTaken in range (1, 7):
            print ('Take a guess.')
            guess  = int (input ())
            
            if guess < secretNumber:
                print ('Your guess is too low.')
            elif guess > secretNumber:
                print ('Your guess is too high.')
            else:
                break # This condition is for the correct guess

        if (guess == secretNumber and guessesTaken == 1):
            print ('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guess.')
            break 
        elif (guess == secretNumber and guessesTaken > 1): 
            print ('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.')
            break
        else:
            print ('Nope. The number I was thinkig of was ' + str(secretNumber))
            break

    except ValueError:
        print ('You did not enter a number.') # This condition is for if a non-integer is entered.
    

I feel like I've tried every combination of where I can but the try and for loop, but can't seem to figure it out.



Solution 1:[1]

This is a guess the number game.

import random

print ('Hello, what is your name?') name = input () guess = 0

name = name.strip()

print ('Well, ' + name + ' I am thinking of a number between 1 and 20.') secretNumber = random.randint (1, 20)

print ('DEBUG: Secret number is ' + str(secretNumber))

while True: for guessesTaken in range (1, 7): try: print ('Take a guess.') guess = int (input ())

        if guess < secretNumber:
            print ('Your guess is too low.')
        elif guess > secretNumber:
            print ('Your guess is too high.')
        else:
            break # This condition is for the correct guess
    except ValueError:
        print ('You did not enter a number.') # This condition is for if a non-integer is entered.


if (guess == secretNumber and guessesTaken == 1):
    print ('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guess.')
    break 
elif (guess == secretNumber and guessesTaken > 1): 
    print ('Good job, ' + name + '! You guessed my number in ' + str(guessesTaken) + ' guesses.')
    break
else:
    print ('Nope. The number I was thinkig of was ' + str(secretNumber))
    break

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CHDSA