I am trying to:
Implement an iterative Python function which returns the sum of the first n integers

so far i have this:

i = 0
total = 0

def sum(n):
            while i <> n:
	      total = total + 1;
	      i = i + 1;
	      return total

then later when i try to used sum(5) i get this error message:

Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
File "<pyshell#7>", line 2, in sum
while i <> n:
UnboundLocalError: local variable 'i' referenced before assignment

now if i change my code from total = total +1 to just total +1
(and the same for i = i +1)
that error message doesnt come up anymore but the answer to any sum(n) is always 0.

I am confused as to why this is happening.
local variable 'i' referenced before assignment probably has something to do with it but if someone could explain why this is happening id be very grateful.

Thanks in advance and try to keep it simple for me ^^ haha

9 Years
Discussion Span
Last Post by scru

take the initializers and put them inside the function body, so that you have:

def sum(n):
    i = 0
    total = 0
    ...rest of your code starting with while statement

Although, I have to ask, what is the purpose of this function? As it is, it will always return n.

Another thing, try to use the standard 4 (or 8) spaces for indentation. Avoid tabs and other non-standard forms like the plague. And never mix different forms in the same program.


Well thats a simple and very helpful response and i understand it for future reference.

Oh the function is supposed to get the number you put in n and then go into the loop, every time it goes around the loop adding i to the total and repeating until i reaches n.

Oh in python i always use the tab button.

On here though you cant use tab cause it'll leave the text box, i guess i should stick to a format when using space bars.
I choose......... 4 spaces! thanks for the advice there( i always do try to avoid the plague.)

I'll put the initializers inside the function body and then try to edit add in some stuff to get it working properly, i'm confident i can do this. But i'll leave the thread open until i've done it.

Thanks again


Haha i did it, but i had even more help from you, when you asked what did my function actually do and i explained it to you, i had a good think when writing it and i understood why it didnt work.

i was able to change my code. here it be.

def sum(n):
    while i<>n:
        i = i+ 1;
        total = total + i;
    return total

I'm am happy that this is correct.

I'll leave this for a couple of days, should i take the answer down after so others dont just copy and paste?

Thanks again for your help and understanding


Some text editors will automatically convert your tabs to spaces for you (python IDLE does this, I think), or can do it with a command.

As for the answer, I don't see a problem with leaving it there. While I don't usually encourage people being lazy and copying down homework from the internet, it probably won't hurt you (can't say the same for them) if they do.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.