最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How do I make a function restart? Specifically I'd like the "call_count" log to return to the

programmeradmin1浏览0评论

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)


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)


Share Improve this question edited Feb 6 at 14:23 Christoph Rackwitz 15.4k5 gold badges39 silver badges51 bronze badges asked Feb 6 at 10:45 D3AD_SHOTD3AD_SHOT 316 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论