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!

3 posts were split to a new topic: What do the instructions mean?

2 posts were split to a new topic: Seems right, what am I missing?

2 posts were split to a new topic: Why doesn’t this work?

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

6 posts were split to a new topic: Where should return be placed in my solution?

3 posts were merged into an existing topic: How can I get the sum of all values in a list?

A post was split to a new topic: Why doesn’t this code work?

A post was merged into an existing topic: How can I get the sum of all values in a list?

Please could someone explain why the solution to this exercise produces the same result as my solution? I was under the impression you had to use a nested loop so that the outer loop can loop through lst1 and the inner loop can loop through lst2

Exercise solution:

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

My solution:

def larger_sum(lst1, lst2):
  sum1 = 0
  sum2 = 0
  for number_lst1 in lst1:
    for number_lst2 in lst2:
      sum1 += number_lst1
      sum2 += number_lst2
  if sum1 >= sum2:
    return lst1
  else:
    return lst2

Obviously the solution to the exercise is more concise and I just want to understand how you can ignore this whole inner loop / outer loop issue

Thanks!

I ran this code, and it gave the right answer, but Codeacademy said it returned the wrong list, but it didn’t.

#Write your function here

def larger_sum(lst1,lst2):
y=0
x=0
for num in lst1:
x=num+x
for num1 in lst2:
y=num1+y
if x>y:
return lst1
else:
return lst2

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

We can ignore it because it is not necessary. The two lists have no need of interaction, so can (should) be summed up separately, as shown in the solution.

If we examine the nested list example, sum2 will accumulate as many times as there are items in lst1. That means that if there are ten items in lst1, sum2 will be ten times as large as as lst2's actual sum.

What is the error message, exactly?

Getting a name error while trying to run the code

Hi

Ive been trying to get trough to this excercise, but it says one of the variable names in the for loop is not defined.

Am I missing something here?

Below is the code, would really appreciate it if someone could take a look

def larger_sum(lst1, lst2):
  sum1 = 0
  sum2 = 0
  for h in i:    # i is not defined (use lst1)
    sum1+=1
  for j in k:    # k is not defined (use lst2)
      sum2+=1
  if sum1 > sum2 or sum1 == sum2:
    return sum1
  else:
    return sum2

That will take care of the variable names. There is another issue, though. Should we be adding 1 each time, or should we add h in one sum, and j in the other?

Last, although minor,

if sum1 >= sum2:
2 Likes

For the Code Challenge: Loops, my answer was:

def larger_sum(lst1, lst2):
  sum_list1 = 0
  sum_list2 = 0
  for n in lst1:
    sum_list1 += n
    n += 1
  for n in lst2:
    sum_list2 += n
    n += 1
  if sum_list1 >= sum_list2:
    return lst1
  else:
    return lst2

While it worked, I noticed this piece was not needed:

n += 1

Is this because the loop function iterates through lst1 and lst2 regardless of this line of code? When would this be needed in a loop function?

You are correct that your for loop will progress through the iterable (in this case a list) without any need for incrementing. Try using a print statement or similar to determine what value n takes on each iteration of the loop.

*As an aside if this this is the same lesson I think it is double check the second requirement on what value to return-

It is commonly used in other languages as an incremental counter, either to check how many times a loop repeats or as a method of accessing every variable in an array. There are numerous valid uses of incrementing values in python too. If it’s just being used as an incremental counter it would be worth looking up the enumerate function for future usage, but it is not necessary as part of this section of the codeacademy lessons-
https://docs.python.org/3/library/functions.html#enumerate

A quick example-
test_list = ['a', 'b', 'c']
for n, item in enumerate(test_list):
  print(f"iteration = {n}, item value = {item}")

Hi, I have a question because I am confused about the feedback when I run the script.

My Code:

def larger_sum(lst1, lst2):
  sum1 = 0
  sum2 = 0
  for num in range(0, len(lst1)):
    sum1 = sum1 + lst1[num]

  for num1 in range(0, len(lst2)):
    sum2 = sum2 + lst2[num1]
  
  if sum1 > sum2 and not 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]))

This is the solution proposed code:

#Write your function here
def larger_sum(lst1, lst2):
  sum1 = 0
  sum2 = 0
  for number in lst1:
    sum1 += number
  for number in lst2:
    sum2 += number
  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 think(I hope), that my code is correct. If so why I reveiving the following feedback error?

larger_sum([1, 9, 5], [2, 3, 10]) should have returned [1, 9, 5] , and it returned [2, 3, 10]

I don’t understand because in the output panel I receive this output:

[1, 9, 5]

Can someone please tell me the difference between my code and the solution proposed?

You’d need to use the same input as the one the error message if you wanted to reproduce that situation. If you use a different input… yeah you’ll get a different output.

(also, you can make your function simpler by breaking out sum into a function

def sum(xs):
    total = 0
    for x in xs:
        total += x
    return total

def larger_sum(a, b):
    if sum(a) >= sum(b):
        return a
    return b

If there’s less to look at you’ll have an easier time reasoning about it.

yes thanks! After hours of studying my brain is missing the easy things…
So I figured it out that the error was this line:

  if sum1 > sum2 and not sum1 == sum2:

Thanks!