Pregunta

I'm new to programming and i just started using this website. I can't find anything much that is similar to my simple problem. i'm using Phyton 3.3. I'm creating a code that measures solar intensity on solar panels. I did well initially, but i decided to make the equations more complex to better reflect real world situations.

I'm putting a simplified equation similar to the problem i encountered:

total=0
def equation():
    x=2+i
    total+=x             
    print (total)

for i in range(1,32): #represents no of days in January (31 days)
    equation()
for i in range (32,61): #represents no.of days in February (28days)
    equation()

etc....for all the months

This is the problem("total+=x). I cannot add all the results from x. The error says that "total" is referenced before assignment. Now i could reference it within the function but then it would just give me results (x) for each iteration, not the SUM of all iterations.

The real equation contains more than 15 lines of formulas. I want to insert the equation onto different ranges (all 12 months of the year). I don't want to copy and paste this huge formula under each range. It will look messy. I would prefer a more efficient solution and reuse the code. So far i'm scratching my head.

I've also considered using the built-in SUM function instead of "total+" and finding various ways using the 'while' loop to circumvent the problem. No luck

I'm learning about classes and using OOP in Python. Still trying to understand the concepts. However i believe the basics should cover this problem. But i don't know how. I've been struggling at this for days trying to work around the problem.Your help is very much appreciated.

If i am not being very clear, please forgive me. I will try to clarify any questions you have.

Thanks

Zac

¿Fue útil?

Solución 2

You should change the function to take the i as input. And then move the summation out of the function.

total=0
def equation(i):
    x=2+i
    return x

for i in range(1,32): #represents no of days in January (31 days)
    total += equation(i)
print total 

Or you could do it this way with list comprehensions.

#OR 
total += sum(equation(i) for i in range(1,32))
print total

Otros consejos

Do you mean the following code? It seems like you forgot the i parameter in the function equation()...

total=0
def equation(i):
    global total
    x=2+i
    total+=x             
    print (total)

for i in range(1,32): #represents no of days in January (31 days)
    equation(i)
for i in range (32,61): #represents no.of days in February (28days)
    equation(i)

But I would write it like this:

def equation(x):
     return x + 2

total = sum(map(equation, range(1,32)))
total += sum(map(equation, range(32,61)))

print total # 1830

Or if you have simple equation (which fit in one line):

total = sum(map(lambda i: i + 2, range(1,32)))
total += sum(map(lambda i: i * 2, range(32,61)))

print total # 3226

If you're assigning to a global variable inside a function, you need to declare it as global inside that function.

def equation():
    global total

However, although this will fix that error, there are other issues with this code as other people have noted.

def equation():
  x=2+i

Here's your problem. The i in that function is not the same i in

for i in range(1,32):

The i in equation refers to some other previously-defined i variable not shown in your code. The for loop creates a new i different from the one that equation is referencing, but doesn't pass it to equation.

Change equation to

// note the argument
def equation(i):

Then equation will take the i from the for loop as an argument and it'll work.

You need to use the global variable declaration in Python. You can try the following code:

total=0
def equation(i):
 x=2+i
 global total
 total+=x

for i in range(1,32): #using January as an example
 equation(i)
print total

Please be careful with undesirable side effects while using a variable set to global.

It's probably best to use the following:

def equation(i):
x=2+i

def sum_results(s,e): # where s is the starting point and e is the end point
total=0
for i in range(s,e+1):
 total+=equation(i)
print total
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top