Python Forum

Full Version: Help with chaos function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Im taking a Intro to Programming Course for my Associate's in Computer Information Systems. The book im using is Python Programming, Intro to Computer Science by John Zelle. Im using Python Idle Shell 3.11.4. In the first chapter it gives an example of a chaos function, and then asks questions on how to make changes or improvements to it to provide different results.

The one im stuck on asks to take two inputs and display two columns side by side. Ive got it to do that, but in the first column it starts producing the same number, where as the second column contains chaos numbers. In a single instance of the math part it works fine but when I add the second math part, it makes the first column just display the same number over and over again.

Im not sure what's not quite right as i've changed variable names and added redundancy for them to try and make sure the number to be processed isn't getting stuck or looped somewhere, which it seems like it's doing. Can anyone give me a hint as to what line or change needs to be made so both columns work independently of each other but still be in the same over all function? Code below:


def main():
    print("This program illustrates a chaotic function")
    n=eval(input("how many numbers should I print?"))
    o=eval(input("how many numbers should I print?"))

    x=eval(input("Enter a number between 0 and 1: "))
    y=eval(input("Enter a number between 0 and 1: "))
    
    for i in range(n):
        x=2.9*x*(1-x)

   
    for i in range(o):
            y=3.9*y*(1-y)
            print(x,y)
And I get results like this:
Output:
main() This program illustrates a chaotic function how many numbers should I print?6 how many numbers should I print?6 Enter a number between 0 and 1: .2 Enter a number between 0 and 1: .3 0.694155708424637 0.819 0.694155708424637 0.5781321000000001 0.694155708424637 0.951191962303401 0.694155708424637 0.18106067129594494 0.694155708424637 0.5782830479626462 0.694155708424637 0.9510998811665442
How should the output look in n and o are different?
The first column of variable x should look like the second column of variable y, with the random numbers representing a chaos output. It does just that then when im only using one column. It's only when I add the second column to the code and request it's output, does the first column of variable x begin to output the same number result instead of chaos, while the column of variable y behaves as expected with it's output, random chaos numbers.
Could you provide an example instead of an explanation? Something similar to:

Output:
If n = 2 and o = 3 I would expect my table to look something like this: 0.694155708424637 0.819 0.951191962303401 0.5781321000000001 0.18106067129594494
Try answering my question like that. How should the tables look if the user enters different numbers for n and o?

Regardless of the answer, I don't think your program will use 2 loops.

That is a pretty old book (2003). Python has changed a lot since then. Python has changed quite a lot since I started learning it in 2018. Do you have to use that book? For example, nobody would use eval() in this program. They would use int(). Same goes for IDLE. There are many free Python IDE's that are much better.
Ok It seems to be related to my print function, as when I print after each calculation it works fine, just need to figure out how to make them print side by side. Don't tell me :P

def main():
    print("This program illustrates a chaotic function")
    n=eval(input("How many numbers should I print?"))
    o=eval(input("How many numbers should I print?"))

    x=eval(input("Enter a number between 0 and 1:"))
    y=eval(input("Enter a number between 0 and 1:"))
    
    for i in range(n):
        x=2.9*x*(1-x)
        print(x)
   
        
    for i in range(o):
        y=3.9*y*(1-y)
        print(y)
Output:
main () This program illustrates a chaotic function How many numbers should I print?6 How many numbers should I print?6 Enter a number between 0 and 1:.2 Enter a number between 0 and 1:.3 0.46399999999999997 0.7212416 0.583051247845376 0.7049972216708452 0.6031308034109796 0.694155708424637 0.819 0.5781321000000001 0.951191962303401 0.18106067129594494 0.5782830479626462 0.9510998811665442
The book is 2016 (Third Edition) and it's the one I purchased right from the college course link through their bookstore.
I don't know what eval() does, since I never used it. eval() seems to be a security related measure.

I'm not really sure what you need, maybe just nice tidy output?

tabulate is a module for taking lists and other iterables and presenting the output tidily. I remember menator mentioned it quite a while ago.

from tabulate import tabulate

def dump():
    tups = []
    print("This program illustrates a weird but useless function.")
    n = int(input("How many numbers should I print for x and y values? ")) 
    x = float(input("Enter a number for x between 0 and 1: "))
    y = float(input("Enter a number for y between 0 and 1: "))
    tups = [('X values', 'Y values')]    
    for i in range(n):
        x=2.9*x*(1-x)
        y=3.9*y*(1-y)
        tup = (x, y)
        tups.append(tup)
    return tups

table = dump()
print(tabulate(table))
print(tabulate(table))
Output:
------------------ ------------------- X values Y values 0.54375 0.88725 0.71944921875 0.39014600625 0.5853419171310424 0.9279351902229824 0.703878595823155 0.26079874457839736 0.6044572026790179 0.751852761678444 0.6933572091445763 0.7276237270962695 0.6165776700482796 0.7729310115649317 0.6855879758537319 0.6844837854119581 0.6251155993336924 0.8422663583882909 0.6796036517297757 0.5181295856709103 ------------------ -------------------
Maybe that helps?
(Aug-23-2023, 07:19 AM)Pedroski55 Wrote: [ -> ]I don't know what eval() does, since I never used it. eval() seems to be a security related measure.
You are absolutely right, a line such as
eval(input('How many numbers do you want?'))
will execute any syntactically correct Python expression that the user may enter, for example the user could list my home directory:
>>> eval(input('How many numbers do you want? '))
How many numbers do you want? print(__import__('os').listdir(__import__('os').path.expanduser('~')))
... # this prints my home directory's content
But the user could do many other things... Sick

The solution is to use int() instead of eval()
int(input('How many numbers do you want?'))
eval is lesser evil than exec.

eval should only evaluate Literals.
exec can import and execute code.

eval is used in Jinja2 to fill the templates with data because it's faster than dispatching the different types in Python Code. But the author knows what he is doing. Using as a beginner eval is in the most cases not required.
(Aug-23-2023, 09:02 AM)DeaD_EyE Wrote: [ -> ]eval is lesser evil than exec.
It is not that obvious:
eval('exec("""import os\nprint(os.listdir(os.path.expanduser("~")))""")')
Pages: 1 2