Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!feeder3.eweka.nl!81.171.88.16.MISMATCH!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; 'calculates': 0.09; 'terminate.': 0.09; 'termination': 0.09; 'def': 0.10; 'stack': 0.15; 'iteration': 0.16; 'iteration.': 0.16; 'itertools': 0.16; 'loops': 0.16; 'lot!': 0.16; 'simplified': 0.16; 'still,': 0.16; 'temp': 0.16; 'wrote:': 0.17; '(or': 0.18; 'otherwise,': 0.20; 'parameters': 0.20; 'import': 0.21; 'runs': 0.22; 'for?': 0.23; 'script': 0.24; 'specifically': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'implies': 0.29; 'way?': 0.29; 'case,': 0.29; 'print': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'but': 0.36; 'too': 0.36; 'uses': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'more': 0.63; 'hello!': 0.65; 'received:74.208': 0.71; '10:32': 0.84; 'recursion,': 0.84; 'room).': 0.84; 'temperature': 0.84; 'temperature,': 0.84; 'ana': 0.91 Date: Tue, 05 Mar 2013 11:02:03 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Recursive function References: <9ff73ed4-24cd-4a61-bb54-a67dd4a96ed0@r9g2000vbh.googlegroups.com> In-Reply-To: <9ff73ed4-24cd-4a61-bb54-a67dd4a96ed0@r9g2000vbh.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:lcR0Bjk3HLs47MlI6IHA/nag4z+HCBvIie8E29YeGtt MGaXlkYjfgBGZ6I8rRTJJwht3DNNvF22+5XudwWnQAZn0N+sZC 1ZAJotVRIOR7lsmPjC/HpZD+z5wY68pwrCA3CV/9TmEiM6naXi ehkCDiqX0i6lptkbi+kuYanNUrfIAw+4SAHM97XGCPNTgYwL8X MJojBbaX8mwDct8tlr8YSAt+QCn1tylFQqF6jeDSqNtn8Ahujx x42YPGxwkYOsE7NtmcTChWVA0AOAxwf2/4it5caHZFnxnpG+7W SCVOsnvMZx2GQWhWh0FyVEJNE0mh1VH3mWc4p97u7YwKbzapw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1362499338 news.xs4all.nl 6967 [2001:888:2000:d::a6]:57411 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40539 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