'Counting and Error Handling Block Guessing Game

Could you, please, help me to understand how should I use try/except block and count tries at the same time. Here is the code without try/except block (it seems it's working fine):

import random

number = random.randint(1, 10)
tries = 3
name = input('Hi! What is your name?\n')

answer = input(f'{name}, let\'s play a game! Yes or No?\n')

if answer == 'Yes':
    print(f'But be aware: you have only {tries} tries!\nReady?')
    chat = input('')
    print('Ok, guess a number from 1 to 10!')
    
    while tries != 0:
        choice = int(input('Your choice: '))
        tries -= 1
        if choice > number:
            print('My number is less!')
        elif choice < number:
            print('My number is higher!')
        else:
            print('Wow! You won!')
            break
        print(f'You have {tries} tries left.')
    if tries == 0 and choice != number:
        print(f'Sorry, {name}, you lost... It was {number}. Try next time. Good luck!')
else:
    print('No problem! Let\'s make it another time...')

This one is with try/except block.. Not sure where should I place 'choice' variable and where count 'tries', it keeps looping and looping:

import random

number = random.randint(1, 10)
tries = 3
name = input('Hi! What is your name?\n')

answer = input(f'{name}, let\'s play a game! Yes or No?\n')

if answer == 'Yes':
    print(f'But be aware: you have only {tries} tries!\nReady?')
    chat = input('')
    print('Ok, guess a number from 1 to 10!')
    
    while True:
        try:
            choice = int(input('Your choice: '))
            if 0 < choice < 11:
                while tries != 0:
                    tries -= 1
                    if choice > number:
                        print(f'My number is less!')
                    elif choice < number:
                        print(f'My number is higher!')
                    else:
                        print('Wow! You won!')
                        break
                    print(f'You have {tries} tries left.')
                if tries == 0 and choice != number:
                    print(f'Sorry, {name}, you lost... It was {number}. Try next time. Good luck!')
            else:
                print(f'Hey {name}, I said, print a number from 1 to 10!')

        except ValueError:
            print('Please, enter a number!')

else:
    print('No problem! Let\'s make it another time...')

Thanks!



Sources

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

Source: Stack Overflow

Solution Source