pythonpython-3.xfunctionrangeiterable

function to sum range gives: 'int' object is not iterable


I'm trying to write a function where the user gives the start and end of a range and the output is that range summed. This is my best try:

def sum_range (start, end):
    output = 0
    userange = range(start, end)
    for i in userange :
        sum(i, output)

    return output

I get the following error:

TypeError: 'int' object is not iterable


Solution

  • you can do :

    def sum_range(start, end):
        return sum(range(start, end))
    

    example :

    >>> def sum_range(start, end):
    ...     return sum(range(start, end))
    ... 
    >>> sum_range(10,14)
    46