-1

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!

Mr. Discuss
  • 355
  • 1
  • 4
  • 13
STRONG95
  • 23
  • 4
  • 1
    Reformat your code. – nicomp Aug 09 '20 at 18:10
  • Everything after a return statement is not executed any more. If you want to get both data from `l` and `s`, you have to save it and return it both at the end, see also here: https://stackoverflow.com/a/7129293/2648551 – colidyre Aug 09 '20 at 18:22
  • The first version of the function doesn't return *anything*, it only prints things — so your question desn't quite make sense… – martineau Aug 09 '20 at 19:33

1 Answers1

0

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 :)

pitamer
  • 905
  • 3
  • 15
  • 27