Reputation: 33
I'd like to make a function that restarts once it hits a certain number, as an example: if call_count > 7: Return to 1 or 0, is this possible? and if it is can anyone help? Thanks
if call_count > 7:
try:
location = pyautogui.locateOnScreen('1.png', confidence=0.9)
if pyautogui.locateOnScreen('1.png', confidence=0.9):
global call_count
call_count += 1
logging.info("Found x1 " + str(call_count) + " amount of times ")
if call_count == 7:
print('Hi')
except pyautogui.ImageNotFoundException:
pass
*Edited Code
def logger():
logging.basicConfig(level=logging.INFO, filename="anomaly.log", filemode="w", format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
logger.propagate = False
handler = logging.FileHandler('anomaly.log')
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
try:
location = pyautogui.locateOnScreen('1.png', confidence=0.9)
if pyautogui.locateOnScreen('1.png', confidence=0.9):
global call_count
call_count += 1
logging.info("Found x1 " + str(call_count) + " amount of times ")
if call_count == 7:
print('Hi')
if call_count > 8:return 1
except pyautogui.ImageNotFoundException:
pass
# schedule for main working code
schedule.every(1).seconds.do(logger)
while True:
schedule.run_pending()
time.sleep(1)
Upvotes: 1
Views: 57
Reputation: 80
I am not sure, what exactly you want to do 7 times and then repeat, but this is how the general structure could look.
The function takes a boolean and then prints a statement, while the count is less than or equal to 7. After that, if the boolean is True, the program stops. If it isn't, it calls itself again with the boolean as True.
Because that boolean is False by default, it does not need to be passed the first time.
def restarting_function(has_restarted=False):
call_count=0
while call_count<=7:
print("Does something with count", call_count)
call_count+=1
if has_restarted:
return
restarting_function(True)
restarting_function()
To add, I would've preferred to do a comment (can't because <50rep), so if my answer does not fit, please tell me and I will delete as soon as possible.
Upvotes: 1