| | |
Help with beginner question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
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
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 "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
1
#2 29 Days Ago
python Syntax (Toggle Plain Text)
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 "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()
•
•
•
•
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
>>>
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
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
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...
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...
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...
Python Syntax (Toggle Plain Text)
# 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 "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...
![]() |
Similar Threads
- beginner question (Assembly)
- Beginner question: Changing contentplaceholder on button click (ASP.NET)
- c++ beginner question one dimensional arrays (C++)
- beginner question (PHP)
- C++ beginner's question (C++)
- A Basic Question. . . (Windows 95 / 98 / Me)
Other Threads in the Python Forum
- Previous Thread: Interesting dilemma - See if you can help.
- Next Thread: Comparing two files line by line
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop windows word wxpython






