samedi 2 septembre 2017

Having trouble understanding python output

Why does the second print lookup method return a blank and not the link acm.org? The first result makes sense but shouldn't the second result be similar?

# Define a procedure, lookup,
# that takes two inputs:

# - an index
# - keyword

# The procedure should return a list
# of the urls associated
# with the keyword. If the keyword
# is not in the index, the procedure
# should return an empty list.


index = [['udacity', ['http://udacity.com', 'http://npr.org']],
         ['computing', ['http://acm.org']]]

def lookup(index,keyword):
    for p in index:
        if p[0] == keyword:
            return p[1]
        return []     



print lookup(index,'udacity')
#>>> ['http://udacity.com','http://npr.org']

print lookup(index,'computing')

Results:

['http://udacity.com', 'http://npr.org']
[]

Aucun commentaire:

Enregistrer un commentaire