FAQ: Code Challenge: Loops - Larger Sum

This community-built FAQ covers the “Larger Sum” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Larger Sum

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello,
I am currently working on exercise #7 in Code challenge - Loops (Python in computer science path).
I don’t understand the output that my function is supposed to have .

In the instruction, it is written:

Create a function named larger_sum() that takes two lists of numbers as parameters named lst1 and lst2 .

The function should return the list whose elements sum to the greater number. If the sum of the elements of each list are equal, return lst1 .

I have put in italic the part that I don’t understand.

In the exercise, we test the function on the list: ([1, 9, 5], [2, 3, 7])

It could be a big help if I could see the final list I should get from the function.

Hi,

Since the sample lists we are using is List 1 - [1, 9, 5] and List 2 - [2, 3, 7], we should use the for loop to count the sum of numbers in each list first.

Then compare both sums and return the greater one. Or if they are equal, return lst1.

def larger_sum(lst1, lst2):
sum1  = 0 #create new variable for sum of numbers from list 1
sum2  = 0 #create new variable for sum of numbers from list 2
  for number in lst1:
     sum1 += number
  for number in lst1:
     sum1 += number
  if sum1 >= sum2:
     return lst1
  else:
     return lst2

We could also use the built-in sum() function:

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

This is what I get, the solution seems to be right, but it still gives me a warning:

codeacademy_solution

Anyone knows why?

line 11:

if summ1 > summ2:

It only returns summ1 if summ1 is larger than summ2.
Else it returns summ2

You should try using >= so that if summ1 is the same as summ2, it will return summ1.

Hi there!

Could you please tell me where I am wrong?

image

Many thanks,

I’m pretty sure it’s because your second for is indented instead of inline with the first one. The second for the if and the else should be inline with the first for. You probably already figured this out.

Here’s my solution

def larger_sum(lst1, lst2):
  sum1 = 0
  for num in lst1:
    sum1 = sum1 + num
  
  sum2 = 0
  for num in lst2:
    sum2 = sum2 + num
    
  if sum1 >= sum2:
    return lst1
  if sum1 < sum2:
    return lst2

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.