Where should return be placed in my solution?

I can’t get sum1 in the below code to iterativley add. I think it must have something to do with the indentation but I tried writing and rewriting it the same way I did for sum2, and for some reason it’s not working. Any help is appreciated!

#Write your function here
def larger_sum(lst1,lst2):
  sum1 = 0
  sum2 = 0
  for x in lst1:
    sum1 += x
    for y in lst2:
      sum2 += y
      if sum1 >= sum2:
        return lst1 
      else:
        return lst2
      
#Uncomment the line below when your function is done
print(larger_sum([1, 9, 5], [2, 3, 7]))

Your for loop never gets to loop! Remember that return, when reached, halts execution of the function.

Put in some print() statements to see what is happening:

def larger_sum(lst1,lst2):
  sum1 = 0
  sum2 = 0
  for x in lst1:    
    sum1 += x
    print("sum 1 = ", sum1)
    for y in lst2:
      sum2 += y
      print("sum 2 = ", sum2)
      if sum1 >= sum2:
        return lst1 
      else:
        return lst2

#Uncomment the line below when your function is done
print(larger_sum([1, 9, 5], [2, 3, 7]))

Now run it.

Change your indentation so that:

  1. The lst1 loop runs
  2. When it is finished, the lst2 loop runs
  3. When that one is finished, the if-else chain runs.

Just because you can nest loops does not mean that you should do so every time!strong text

Thanks.

I rewrote the code so that all the FOR, IF and ElSE lines had the same indentation and it work.

I wanted to try and print sum1 and sum2 just to see that they were adding correctly but everytime i put it beneath the FOR statements it got caught in the loop. Couldn’t figure out where to place a print statement within the function and have it work.

My understanding is that this version works because the IF statement and it’s associated RETURN are now outside the FOR statements.

def larger_sum(lst1,lst2):
  sum1 = 0
  sum2 = 0
  for x in lst1:
      sum1 += x
  for y in lst2:
      sum2 += y
  if sum1 >= sum2:
      return lst1
      print(sum1)
  else:
      return lst2

print(larger_sum([1, 9, 5], [2, 3, 7]))
1 Like

If you want to watch the process unfold, put print() within a loop; if you want to see the final product, put it outside the completed loop. You can add additional information to what you are printing, like this:

...
  for x in lst1:
      sum1 += x
      print("x, partial sum1 = {}, {}".format(x,sum1))
  print("final sum1 {} = ".format(sum1))
...
# Output: for lst1 ==  [1, 9, 5]
x, partial sum1 = 1, 1
x, partial sum1 = 9, 10
x, partial sum1 = 5, 15
final sum1 = 15

Exactly. Now the if - elif chain can work with the completed sums.

1 Like

Hi so I was wondering why my code does not work when I return each time I add the sum:

#Write your function here
def larger_sum(lst1, lst2):
  sum1 = 0
  sum2 = 0
  for num in lst1:
    sum1 += num
    return sum1
  for num in lst2:
    sum2 += num
    return sum2
  if sum1 >= sum2:
    return lst1
  else:
    return lst2

#Uncomment the line below when your function is done
print(larger_sum([1, 9, 5], [2, 3, 7]))

I eventually got the code to work by checking out the solution to the exercise after a lot of frustration lol but I would like to know whats going on behind the scenes that causes it not to work when I use return.

return does two things:

  1. It returns a value to the calling statement
  2. It halts processing of the function
1. def larger_sum(lst1, lst2):
2.   sum1 = 0
3.   sum2 = 0
4.   for num in lst1:
5.     sum1 += num
6.     return sum1
... etc
  • So, after line 4 executes one time, and passes num, the first number in lst1, to line 5.

  • The variable sum1 is updated in line five.

  • Then in line 6, that variable sum1 (at this time remember, simply the first number in lst1) is returned to the calling statement,
    print(larger_sum([1, 9, 5], [2, 3, 7]))
    … which prints that number.

  • And then the function stops running. Any local variables it once contained are gone forever, and the function disappears from the call stack (the area in memory where actual processing is taking place) until called again.

Why isn’t this a solution? When i run the code, i get the right result in the console.

def larger_sum(lst1, lst2):
if sum(lst1) > sum(lst2):
return lst1
elif sum(lst1) == sum(lst2):
return lst1
else:
return lst2

#Write your function here
def larger_sum(lst1,lst2):
if sum(lst1)>sum(lst2):
return lst1
elif sum(lst2)>sum(lst1):
return lst2

#Uncomment the line below when your function is done
print(larger_sum([1, 9, 5], [2, 3, 7]))

why is this wrong, it returns [1,9,5]
but the thing says that it returns none, not sure why this is happening

edit: nevermind it was because i didnt have an else stating to return lst1

Hi folks, first time posting here.

In this code challenge, I have been struggling mightily on every exercise until this one, which was just a huge relief. As I’d come to the forums here to read and get help on the prior exercises and do not see how I solved this one here, I thought it would be helpful to share what I did. To @garrettherrington332’s point, we only have two paths, so I wrote it as an if/else (without any elif statement). Note also the >= in the ‘if’ line of code:

def larger_sum(lst1, lst2):
  if sum(lst1) >= sum(lst2):
    return lst1
  else:
    return lst2

The little green check mark in the box never looked quite so good when I clicked “Run” as I’ve become so used to getting some error before figuring out the solution… :sweat_smile: