Search code examples
pythonprogramming-languagespython-3.x

writing an improved version of the Chaos Help


Here is the question proposed by the text.

Write an improved version of the Chaos program from Chapter 1 that allows a user to input two initial values and the number of iterations and then prints a nicely formatted table showing how the values change over time. for example, if the starting values were .25 and .26 with 10 iterations, the table would look like so:

following this is a table with a index 0.25 0.26 as headers and then the 10 iterations in two columns.

here is my initial Chaos program.

# File: chaos.py

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

main()

my question is how do i change it to fulfil the above question..

Please if answering take in mind this is my first programming class ever.


Solution

  • You really just have to duplicate the functionality you already have. Instead of just asking the user for an x value, also ask for a y value.

    x= float(input("enter a number between 0 and 1: "))
    y= float(input("enter another number between 0 and 1: "))
    

    Then in your loop you need to do the same thing you did with the x value to the y value. When you print, remember that you can print two values (x and y) at once by separating them with a comma.

    Also, as PiotrLegnica said, you should use float(input(...)) instead of eval(input(...)). Since you know that the user should enter a floating point number (between 0 and 1) you don't have to call eval. Calling eval could be dangerous as it will execute any instruction given to it. That might not matter right now, but it's better to not get in the habit of using it.