I am interested in learning python and picked up a friends textbook to try and work through it. I have no cs experience so am pretty clueless. I am having difficulty with one of the problems in the first chapter lol. i am trying to make the program spit out a loop of 8 numbers based on a simple algorithm. Here is the code:

def main():
print "This program illustrates a chaotic function."
print "Please enter two numbers between 0 and 1."
x = input ("Enter first number: ")
y = input ("Enter second number: ")
print
print "input", x, y
print "-----------------"

for i in range (8):
x = 3.9 * x * (1 - x)
for i in range(8):
y = 3.9 * y * (1 - y)
print x, y

main()

output:

This program illustrates a chaotic function.
Please enter two numbers between 0 and 1.
Enter first number: .25
Enter second number: .25

input 0.25 0.25
-----------------
0.540417912062 0.73125
0.540417912062 0.76644140625
0.540417912062 0.698135010439
0.540417912062 0.82189581879
0.540417912062 0.570894019197
0.540417912062 0.955398748364
0.540417912062 0.166186721954
0.540417912062 0.540417912062
>>>

the first loop is only spitting out the last calculation repeatedly whereas the second is computing correctly so im a bit confused... any help would be appreciated
thanks

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by newportking
``````def main():
print "This program illustrates a chaotic function."
print "Please enter two numbers between 0 and 1."
x = input ("Enter first number: ")
y = input ("Enter second number: ")
print
print "input", x, y
print "-----------------"

for i in range (8):
x = 3.9 * x * (1 - x)
for i in range(8):
y = 3.9 * y * (1 - y)
print x, y

main()
``````

output:

This program illustrates a chaotic function.
Please enter two numbers between 0 and 1.
Enter first number: .25
Enter second number: .25

## input 0.25 0.25

0.540417912062 0.73125
0.540417912062 0.76644140625
0.540417912062 0.698135010439
0.540417912062 0.82189581879
0.540417912062 0.570894019197
0.540417912062 0.955398748364
0.540417912062 0.166186721954
0.540417912062 0.540417912062

I'm adding code and quote tags to your stuff so that it's easier for other forum members to read. Please try to make use of these options when posting, as it will greatly increase the likelihood that you'll get satisfactory responses.

To see the different tags I've used in this post, simply "Reply" to the message and look at what I typed. Also, you can use the tool buttons at the top of the editing window to help with generating tags. There's lots of different BB Tags available on this forum, you'd benefit from using them.

--

You have `print x,y` in your second loop only. Loop 1 modifies the value of x. Loop 2 modifies the value of y. So when you print, you only see the last value of x after loop 1 completed, yet you see the continual changes of y.

Since the loops are exclusive to each other, why not try combining them into a single for loop. Do the calculation on x, then the calculation on y, then print.

HTH

Edited by happygeek: fixed formatting

Ahh I didnt realize i could do that although looking at it i dont see why i shouldnt be able to because it makes sense... thank you for clarifying this for me.

sorry about the formatting i will keep it in mind how to do it correctly... i saw after i posted that i forgot to encase the code...
Here is the new program...

``````# File: chaos2column.py
# A simple program illustrating chaotic behavior.

def main():
print "This program illustrates a chaotic function."
print "Please enter two numbers between 0 and 1."
x = input ("Enter first number: ")
y = input ("Enter second number: ")
print
print "input", x, y
print "-----------------"

for i in range (8):
x = 3.9 * x * (1 - x)
y = 3.9 * y * (1 - y)
print x, y

main()``````

Output:
This program illustrates a chaotic function.
Please enter two numbers between 0 and 1.
Enter first number: .25
Enter second number: .26

input 0.25 0.26
-----------------
0.73125 0.75036
0.76644140625 0.73054749456
0.698135010439 0.767706625733
0.82189581879 0.6954993339
0.570894019197 0.825942040734
0.955398748364 0.560670965721
0.166186721954 0.960644232282
0.540417912062 0.147446875935

Thanks again for your help.. Im sure I will be around with other ridiculous questions...