[Python] Functions within a loop [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    There is something fundamental with the way a try statement behaves within a while loop that seems counterintuitive. In the following code, the await page.find statement fails on the first iteration. The pointer 'index' increments and the try statement is run again. This time, with index = 1, the page.find statement is successful. However, it appears that only the third line beneath the try (play_date = ...) executes. The time1 is not updated and the print statement (print("Attempt #...")) is not executed. After looking at this for a while I still cant understand why some of the lines are not executed.

    Seems like a basic truth that all lines would be executed the second time through, but it is not performing that way. I can't find anything in the documentation or my code that explains this.

    while index < 4:
    try:
    time1 = datetime.datetime.now()
    print("index = ",index, "time =", time1)
    play_date = await page.find(validation_xpath[index])
    time2 = datetime.datetime.now()
    print("Attempt #", index + 1, " time found")
    except asyncio.TimeoutError:
    # Did not get a hit, test for end of loop and send message
    print("Attempt #" + str(index + 1) + " time not found in " + str(time1 - datetime.datetime.now())[-8:-3] + " seconds")
    if index == 3:
    print("MSG E110 - Failed on attempt #", index + 1, " to verify course open - shutting down")
    index += 1
    # Time found - valid date/time/course combination
    print("Time found on attempt # " + str(index + 1) + " in " + str(time1 - datetime.datetime.now())[-8:-3] + " seconds")
    print("MSG E120 - Valid Date/Time/Course Selected on try # ", index + 1," - CONTINUING")


    Here is the output that shows the apparent sequence of execution:

    index = 0 time = 2025-02-12 11:06:32.591297
    Attempt #1 time not found in 9.658 seconds
    Time found on attempt # 2 in 9.658 seconds
    MSG E120 - Valid Date/Time/Course Selected on try # 2 - CONTINUING")


    Revised code follows (see my comment at the bottom for context):

    index = 0
    time0 = datetime.datetime.now()
    while index < 4:
    try:
    time1 = datetime.datetime.now()
    play_date = await page.find(validation_xpath[index], False, True, 3)
    time2 = datetime.datetime.now()
    # print("Attempt #" + str(index + 1) + " Valid selection FOUND in " +
    # str(datetime.datetime.now() - time1)[-9:-3] + " seconds")
    if index == 1 or index == 3:
    event_scheduled = True
    index = 4
    except asyncio.TimeoutError:
    # Did not get a hit, test for end of loop and send message
    # print("Attempt #" + str(index + 1) + " Valid selection NOT found in " +
    # str(datetime.datetime.now() - time1)[-9:-3] + " seconds")
    if index == 3:
    print("MSG E110 - Failed on attempt #", index + 1, " to verify course open - shutting down")
    return
    index += 1

    Continue reading...

Compartilhe esta Página