So let's just set it up retrying modular :
pip install retrying -i https://pypi.tuna.tsinghua.edu.cn/simple
Decorate before the function after importing the package :
from retrying import retry
import random
# I'm going to randomly generate a number , The required return is a value less than 1 The number of
@retry(stop_max_attempt_number=100) #stop_max_attempt_number Represents the maximum number of retries
def fun():
result = random.randint(0, 10)
print(' Current number size :', result)
if result <= 1:
print('*** The current number is less than 1, Can return ***')
return result
else:
raise ValueError
fun()
Output results :
Current number size : 3
Current number size : 2
Current number size : 4
Current number size : 5
Current number size : 7
Current number size : 8
Current number size : 10
Current number size : 4
Current number size : 0
*** The current number is less than 1, Can return ***
Code :
from retrying import retry
import random
# I'm going to randomly generate a number , The required return is a value less than 1 The number of
@retry(stop_max_attempt_number=100) #stop_max_attempt_number Represents the maximum number of retries
def fun():
result = random.randint(0, 10)
print(' Current number size :', result)
if result <= 1:
print('*** The current number is less than 1, Can return ***')
return result
else:
raise ValueError
fun()