Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sum all components and go to next

I was thinking about my code and I think (after debbuging it) it doesnt work good.
In my task I have this short list of numbers:

numbers = [
        2892921,
        1939929,
        2929202,
        1758690,

I have to add to each other my numbers in this way:

1 + 2 + 3 + … + 2892921 = sum, and my for loop should go next: 1 + 2 + 3 + … + 1939929 = sum2 ect.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve written this code. Please give me a sign where can I repeair it to work good?

def sum(__self__):
    suma = 0
    for i, val in enumerate(numbers):
        i += 1
        n = val
        for j in range(1, n):
            suma += j
        print(suma)
    return suma

>Solution :

numbers = [
        2892921,
        1939929,
        2929202,
        1758690
]
def sum_n(number):
  return sum(range(1, number+1))
sums = [sum_n(number) for number in numbers]

print(sums)
print(sum(sums))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading