2

My problem is that I want: and then I want it to get evaluted in the calc_energy function 360 times and then store the result so I could count the year average, I want to calculate the energy average.. Should I call the function instead of return in def_decide(v)??? Could someone help me get in on the right track?

def calc_v(latitud):
    for t in range(0,360):
        v=(23.5*math.sin(math.pi*(t-80)/180)+90-latitud)/90
        decide_v(v):

def decide_v(v):
    if 0<v<1:
       return(v**2)
    if v>=1:
        return(1)
    if v<=0:
        return(0)

def calc_energy(v):
    W=self.area*random(0,1)*self.sundigit*v
    return W

def sort(self,W):
    W+=100/360
2
  • What do you mean with loop through a function? A for loop in a function? Recursion? A function in a for loop? Commented Nov 21, 2014 at 13:09
  • @Pellegrino: please don't vandalize your own questions; that removes a lot of the usefulness of the answers people spent time crafting. Commented Nov 23, 2014 at 0:39

2 Answers 2

3

You can make a generator from calc_v and then use it as you would use a list of values (notice yield instead of return):

def calc_v(latitude):
   for t in range(0,360):
       v=(23.5*math.sin(math.pi*(t-80)/180)+90-latitude)/90
       yield v

def decide_v(v):
    if 0<v<1:
       return v**2
    elif v>=1:
       return 1
    else:
       return 0

for v in calc_v(latitude):
    print decide_v(v)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use recursion which is a function calling itself. Below you can see the function factorial is called within itself. http://www.python-course.eu/recursive_functions.php

def factorial(n):
    print("factorial has been called with n = " + str(n))
    if n == 1:
        return 1
    else:
        res = n * factorial(n-1)
        print("intermediate result for ", n, " * factorial(" ,n-1, "): ",res)
        return res

I get the feeling that you don't really want to know how to loop a function. I think you just want to call your function in the example that you gave.

def calc_v(latitud):
    for t in range(0,366):
        v=(23.5*math.sin(math.pi*(t-80)/180)+90-latitud)/90
        decide_v(v) # return v

def decide_v(v):
    if 0<v<1:
        print(v**2)
    if v>=1:
        print(1)
    if v<=0:
        print(0)

When you use the return statement in a function then you will leave the loop and the function, so your for loop will only run through t=0 then it will break out of the function. I think you just want to call your function and give it the variable v.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.