On 03/05/2013 10:32 AM, Ana Dionísio wrote:
Hello!

I have to make a script that calculates temperature, but one of the
parameters is the temperature in the iteration before, for example:
temp = (temp_-1)+1

it = 0
temp = 3

it = 1
temp = 3+1

it = 2
temp = 4+1

How can I do this in a simple way?

Thanks a lot!


Recursion only works when you have a termination condition. Otherwise, it just loops endlessly (or more specifically until the stack runs out of room). Still, you don't have a problem that even implies recursion, just iteration.

For your simple case, there's a simple formula:

def  find_temp(it):
    return it+3

Methinks you have simplified the problem too far to make any sense.

import itertools
temp = 3
for it in itertools.count():
    temp += 1
    print it, temp

Warning:  that loop will never terminate.

Is this what you were looking for? It uses recursion, and I used <=0 for the termination condition.

def find_temp2(it):
    if it <=0:  return 3
    return find_temp(it-1)


--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to