samedi 3 mars 2018

Recursive if-statement to check search terms against API one at a time in python

Consider the following list of search terms:

searchterms = {'term1': address, 'term2': name, 'term3': 'coordinates'}

where all can be 'nan' or contain some valid search term.

And say I have a function:

def askOracle(term):
    # return query result or status 'NotFound'

how would I in the best and most efficient way start with term1, if the result is nan or NotFound try the next, until one returns a result or all is nan or NotFound.

So far I have ended up with an enormous if elif else block of horror (I'll save you the hassle, it's 7 levels deep).

I'm new to python, but isn't there some short way of doing this? Maybe some recursive function?

Note: I've tried to search both here and google, but I really don't know what to search for.

Aucun commentaire:

Enregistrer un commentaire