简体   繁体   中英

When I swap out return for print, I don' t get all the same values back

when I swap return for print, I do not get all the same values back in my defined function.

Here is my code with print()

def seo():
    sou = soup.findAll(class_ = 'rtf l-row')
    for x in sou:
        l = x.findAll('p')
        s = x.findAll('h4')
        for i in l:
            lolz = i.text
            print(lolz)
        for j in s:
            h = j.text
            print(h)

Here is the exact same code with return:

def seo():
    sou = soup.findAll(class_ = 'rtf l-row')
    for x in sou:
        l = x.findAll('p')
        s = x.findAll('h4')
        for i in l:
            lolz = i.text
            return lolz
        for j in s:
            h = j.text
            return h

when I use return, I only get back the first line of code. Thanks!

There should be only one return statement in a function, and it should be the last statement in it.

You have two return statements inside your seo function. The function reaches the first return statement, and the rest of the code in the function never runs.

You should either break it into two different functions, or return a list or a dictionary so you can have several values returned in a single variable:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM