Help with beginner question

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 2
Reputation: newportking is an unknown quantity at this point 
Solved Threads: 0
newportking newportking is offline Offline
Newbie Poster

Help with beginner question

 
0
  #1
29 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #2
29 Days Ago
  1. def main():
  2. print "This program illustrates a chaotic function."
  3. print "Please enter two numbers between 0 and 1."
  4. x = input ("Enter first number: ")
  5. y = input ("Enter second number: ")
  6. print
  7. print "input", x, y
  8. print "-----------------"
  9.  
  10. for i in range (8):
  11. x = 3.9 * x * (1 - x)
  12. for i in range(8):
  13. y = 3.9 * y * (1 - y)
  14. print x, y
  15.  
  16. 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 use code tags, wrap your code like this:
[code=python]
# Code inside here
[/code]

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.

--

Now onto your dilemma.

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
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: newportking is an unknown quantity at this point 
Solved Threads: 0
newportking newportking is offline Offline
Newbie Poster

Thanks

 
0
  #3
28 Days Ago
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...

  1. # File: chaos2column.py
  2. # A simple program illustrating chaotic behavior.
  3.  
  4. def main():
  5. print "This program illustrates a chaotic function."
  6. print "Please enter two numbers between 0 and 1."
  7. x = input ("Enter first number: ")
  8. y = input ("Enter second number: ")
  9. print
  10. print "input", x, y
  11. print "-----------------"
  12.  
  13. for i in range (8):
  14. x = 3.9 * x * (1 - x)
  15. y = 3.9 * y * (1 - y)
  16. print x, y
  17.  
  18. 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...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC