-1

What I'm trying to do: I'm essentially running a search procedure with a list of IDs. Those that fail for some reason, I append to a separate list so that I can choose to re-run just those under some different parameters.

How I'm trying to do it:

# Defining the customer ID list and a list for failed searches
customer_IDs = [1,2,3,4,5]
failed_IDs = []

# Looping the search procedure for all IDs in the original list
for customerid in customer_IDs:
    try:
        search_procedure(customerid)
    except:
        failed_IDs.append(customerid)
        pass

if len(failed_IDs) > 0:
    print("There were " + str(len(failed_IDs)) + " customers that were not found")

My expectation was that, once the code failed to find customer 1, for instance, it would resume the loop for the remaining 4 and only then go evaluate how long the list is in the if statement. However, after the first ID is not found, it goes straight to the if section.

I have tried to reset the code with both pass and continue, and it's still maintaining the same behavior.

Why I know the loop isn't going forward: Essentially because this is an attempt of web scraping with Selenium. I'm automating a task of searching, navigating and downloading for a series of documents using customer IDs. When the first one breaks, it does not proceed to the second one. The reason the search is failing is because I'm currently unable to properly make Selenium switch to the frame where the necessary, final button is. Unfortunately, there is no error message from the code itself to share with you guys.

I've searched around quite extensively and still found nothing quite similar except for the continue/pass statements on the code, which have not helped.

Any tips for a newbie?

Thanks!

  • 1
    `pass` or `continue` would be completely pointless there, because simply removing them should result in the same behavior, which is the loop continuing until the iterator is exhausted. You have to provide a [mcve], because the code you show wouldn't behave in the manner you describe – juanpa.arrivillaga Nov 13 '19 at 19:15
  • YOu have a very generic Exception Statement. Your logic is correct as per the requirement. To debug , you add print statement in exception block and see , if its going through all the loop. – roshan ok Nov 13 '19 at 19:15
  • Are you *absolutely sure* your code is skipping the loop? Have you tried putting print statements inside the loop to check that it's still getting run (e.g. if there's only one error)? I can't reproduce your error on my machine. – Green Cloak Guy Nov 13 '19 at 19:20

1 Answers1

0

The code seems fine and also you don't need the pass keyword.

By any chance in your editor you have bad indentation? May be if is inside for?

for replication on my side I used the following function and your code

# Dummy function
def search_procedure(idx):

    if idx == 2:
        pass
    else:
        raise ValueError
  • Hello, and thank you for the helpful comment. I have made sure this is no indentation error. In fact, the exact code reads like this: ** Edit: I'm currently unable to add a comment with the adequate code formatting, but it does look quite exactly like the generic code I posted. – Rodrigo M. F. Castilho Nov 21 '19 at 14:47
  • When I run your code I don't get any error. It would be helpful to have the entire code. – mukulgarg94 Nov 22 '19 at 06:28