Aenohe
Aenohe

Reputation: 45

For Loop in Python 3

Don't understand this simple code:

def main():
  print ("This program illustrates a chaotic function")
  x = float(input("Enter a number between 0 and 1: "))
  for r in range(1,10):
    x = 3.9*x*(1 - x)
    print(x)

According to my understanding it should print out 10 identical numbers. But it gives me 10 different. I thought that range (1,10) only means that it iterates trough code 10 times.

Upvotes: 1

Views: 1081

Answers (2)

Marcus
Marcus

Reputation: 6839

range(a,b) returns a tmp list[a, a+1, ..., b-2, b-1], there's no b.

Upvotes: 0

Lev Levitsky
Lev Levitsky

Reputation: 65781

r changes from 1 to 9. x changes from "old x" to "new x" = 3.9*(old x)*(1 - (old x)) 9 times, starting from the input value.

Upvotes: 5

Related Questions