A
A
artemk1ter2021-08-01 12:18:13
Python
artemk1ter, 2021-08-01 12:18:13

How to implement a limited number of except attempts?

Suppose there is a code where the site is accessed via requests.get

Is it possible to somehow implement such logic, in which try except will be executed 10 times, and when the attempts are over, call system.exit (1)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2021-08-01
@artemk1ter

Enter a new variable that will iterate the number of attempts:

counter = 0 # Счётчик
while counter <= 10:
    input_number = int(input('Enter the number: ')) #requests.get()
    try:
        result = 1/input_number
        counter += 1
    except:
        print('Wrong number, try again!')
        counter += 1
else:
    print('End!') #system.exit(1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question