Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40539

Re: Recursive function

Date 2013-03-05 11:02 -0500
From Dave Angel <davea@davea.name>
Subject Re: Recursive function
References <9ff73ed4-24cd-4a61-bb54-a67dd4a96ed0@r9g2000vbh.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2891.1362499338.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 07:32 -0800
  Re: Recursive function Dave Angel <davea@davea.name> - 2013-03-05 11:02 -0500
    Re: Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 08:15 -0800
    Re: Recursive function Ana Dionísio <anadionisio257@gmail.com> - 2013-03-05 08:15 -0800
      Re: Recursive function Neil Cerutti <neilc@norwich.edu> - 2013-03-05 19:05 +0000
  Re: Recursive function Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-03-05 17:06 +0100

csiph-web