BeeGee - 3 months ago 9

Python Question

I was trying to create a function that added up the range of a number, including the number.

So when I input 5, I should receive 15.

Why is this function not working?

`def sum_nums(number):`

start = 0

for index in range(0,len(number)):

start += index

return start

print sum_nums(5)

Answer

This could be done with a 1 liner but i'll help you out with a solution in the format that you're attempting.

```
def sum_nums(number):
total = 0
for i in range(number + 1):
total += i
print total
```

Input `5`

Output `15`

Also, some clarification as per your error message. `len`

is to be used for strings. An integer does not have a length. By simply passing it to the `range`

function it will try all of the numbers (if you don't specify a starting number) from 1 up to that number, but not including it. That's why we do `range(number + 1)`

to include the target number.

Hope this helps :)