I'll throw in a couple of ideas, but I won't pretend to be an expert. ;-)

>I have written what i see as a pretty decent script to resolve this
> question:
>
> 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
> might look like this:
> index 0.25 0.26
> ----------------------------
> 1 0.731250 0.750360
> 2 0.766441 0.730547
> 3 0.698135 0.767707
> 4 0.821896 0.695499
> 5 0.570894 0.825942
> 6 0.955399 0.560671
> 7 0.166187 0.960644
> 8 0.540418 0.147447
> 9 0.968629 0.490255
> 10 0.118509 0.974630
>
> Although it works I am sure I could have gone about this a better way, it
> probably doesn't fit all the rules of best practice either. Was wondering 
> if
> anyone would mind having a look and offering a few tips??
>
> # chaos.py
> # A program to mimic the chaos theory
>
> def main():
>
>    print "Example of Chaos"
>
>    # User inputs numbers to compare, z is for the index counter
>    x = input("Enter a number between 1 and 0: ")
>    y = input("Enter a second number between 1 and 0: ")

Generally, additional checks are made to make sure that the values actually 
are within the proper range, but I wouldn't think that would be necessary in 
such a specific script.

>    z = 0
>
>    # Prints the table borders and titles
>    print '%10s %20s %20s' % ("Index", x, y)
>    print "----------------------------------------------------------"

Can be
print "-" * 58

>    tempx = x
>    tempy = y

You never use x and y again. Why are you changing variables to tempx and 
tempy?

>
>    # Loops calculates 'chaotic behaviour for input numbers
>    for i in range(10):
>        tempx = 3.9 * tempx * (1 - tempx)
>        tempy = 3.9 * tempy * (1 - tempy)
>        z = z + 1
>        # Print chaotice results into table
>        print '%10s %20s %20s' % (z, tempx, tempy)
>
>    raw_input("Press any key to exit")
>
> main()

This is really so short of a script, I wouldn't change the design, but some 
general tips to think about follow...

My biggest worry is whether or not you will change how you print your 
results in a larger piece of code. For example. If you notice, when you 
print out your columns, you have "%10s %20s %20s" in two different places. 
What if you wanted to change the code so that the first column was padded to 
fifteen characters instead of just 10? You would have to change it in all of 
those places and if you missed one~ bam! Bug. The easy way to fix that is to 
assign the pattern to a variable and then you only have to change it once.

pat = "%10s %20s %20s"
print pat % (z,x,y)

Will still work. ;-)

The only other issue I have with this is the issue of the Z counter. In your 
loop you allow i to be a counter from 0 to 9. Why don't you take advantage 
of that? Get rid of the z variable entirely and just use 'i'.

A nice script though. Keep going. 

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to