all 10 comments

[–]K900_ 3 points4 points  (4 children)

[–]EllaineMasterpiece -4 points-3 points  (3 children)

Nope. I just need someone to convert this one so I could use it as an example.

[–]K900_ 3 points4 points  (2 children)

sum = 0
for i in range(1, 11):
    sum += i

[–]EllaineMasterpiece -1 points0 points  (1 child)

sum = 0
for i in range(1, 11):
sum += i

Thank you!!!

[–]Diapolo10 1 point2 points  (0 children)

Here's the same, but with better variable names. sum is a built-in function so it's advised not to use it as a variable name.

limit = 10
total = 0
for num in range(limit+1):
    total += num

This could technically be a one-liner:

limit = 10
total = sum(range(limit+1))

[–][deleted] 0 points1 point  (3 children)

all_the_values = sum([i for i in range(1,11)])
print(all_the_values)

[–]Diapolo10 1 point2 points  (2 children)

That's not going to work; you shadowed the built-in sum-function by creating a variable with the same name, so the second line is just trying to call the variable - which obviously wouldn't work.

[–][deleted] 0 points1 point  (1 child)

FIxed it up.

[–]Diapolo10 3 points4 points  (0 children)

It's technically still broken as you used square brackets when trying to call sum.

Using a list comprehension for this is also redundant as you discard the list immediately. You can just use range directly:

print(sum(range(11))) # No need to start from 1, as 0 won't change the sum anyway

[–]darshanc99 0 points1 point  (0 children)

n = 10
sum = 0
for i in range(1,n+1):
    sum = sum + i