# PURPOSE: to calculate the sum of the first (n) counting numbers
#
# INPUT(S): the number (n) which will be the last number added to the sum
#
# OUTPUT(S): the sum of the first (n) counting numbers
#
# EXAMPLES: input: 4 ; output: 10
#           input: 8 ; output: 36
#
###########################################
import math

def sums(n):
    sum = 0
    for i in range(0, i + 1, 1):
        sum = sum + i
        return sum



def main():
    n = input('Enter a number: ')
    while n < (n + 1):
        s = sums(n)
        while n == (n + 1):
            break
    print 'The sum of the numbers is', s

()

i keep getting an error saying that 'i' was referenced before assignment and i'm not exactly sure where to go from here. any help is appreciated, thanks.

Recommended Answers

All 6 Replies

The i in range(0, i + 1, 1) should be n .

range(n+1) by default goes from zero to n in steps of one

import math

def sums(n):
    sum = 0
    for i in range(0, n + 1, 1):
        sum = sum + n
        return sum



def main():
    n = input('Enter a number: ')
    s = sums(n)
    while n == (n + 1):
            break
            print 'The sum of the numbers is', s



main()

this is the updated code in my program. when i run it the prompt to enter a number shows up but that's it. the program just ends after that. i dont know what im overlooking so enlighten me.

Well, you are close. A test print line will help ...

def sums(n):
    sum = 0
    for i in range(0, n + 1, 1):
        sum = sum + i
        print i, sum  # test
    return sum


def main():
    n = input('Enter a number: ')
    s = sums(n)
    print 'The sum of the numbers is', s


main()

I would avoid using sum for a variable name since it is a Python function name.

# PURPOSE: to calculate the sum of the first (n) counting numbers
#
# INPUT(S): the number (n) which will be the last number added to the sum
#
# OUTPUT(S): the sum of the first (n) counting numbers
#
# EXAMPLES: input: 4 ; output: 10
#           input: 8 ; output: 36
#
################################################################################
import math

def sums(n):
    sum = 0
    for i in range(0, n + 1, 1):
        total = sum + i
    return total



def main():
    n = input('Enter a number: ')
    s = sums(n)
    print 'The sum of the numbers is', s
    while n <= 0:
        print "Please Enter a positive number."



main()

okay i tweaked my code and its almost perfect except that the output is always the same as the input. i'm a little confused and i know the problem is in the sums(n) function, i just don't know what i'm overlooking.

Here is a better sums

def sums(n):
    total = 0
    for i in range(0, n + 1, 1):
        total += i
    return total

Note that python already has a function sum, for example

>>> sum([1, 2, 3, 4])
10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.