[Tutor] functions and iterations
Alan Gauld
alan.gauld at btinternet.com
Tue Nov 13 12:15:43 CET 2012
On 13/11/12 03:56, Rufino Beniga wrote:
> def IterateLogistic(x,r,n):
> for i in xrange(n):
> x = r*(1-x)
> if i = n:
> print x
>
DogWalker has answered your basic question.
But you don't really need the test at all.
Just print x after the loop finishes:
def IterateLogistic(x,r,n):
for i in xrange(n):
x = r*(1-x)
print x
But printing inside a function is usually not the best thing to do.
It's generally better practice to return the value and then print
the result externally:
def IterateLogistic(x,r,n):
for i in xrange(n):
x = r*(1-x)
return x
print IterateLogistic(5,2,4)
It makes your function much more reusable. You can print the result or
store it in a variable for later, or even use it directly in a bigger
more complex expression.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list