Re: [Tutor] Anyone fancy giving me some tips and an expert opinion??

2008-03-18 Thread Kent Johnson
elis aeris wrote:

> for x in xrange (20, 0):
> print x

> but what if I need the for loop to go from a big number to a small number?

Just give a step value:
for x in xrange(20, 0, -1):
   print x

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


Re: [Tutor] Anyone fancy giving me some tips and an expert opinion??

2008-03-18 Thread elis aeris
i am not sure if I am still in the mailing list of tutor@python.org so
please use reply all to send me a email directly please!



x = 0
y = 0
for x in xrange (20, 0):
print x



this doesn't work because it goes from a big number to a small number, which
does nothing



but what if I need the for loop to go from a big number to a small number?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Anyone fancy giving me some tips and an expert opinion??

2008-02-09 Thread Ricardo Aráoz
Damian Archer wrote:
> 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: ")
> z = 0
> 
> # Prints the table borders and titles
> print '%10s %20s %20s' % ("Index", x, y)
> print "-
> -"
> tempx = x
> tempy = y
> 
> # 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()
> 


>From a procedural POV,
UNTESTED :


# chaos.py
# A program to mimic the chaos theory

def main() :
print "Example of Chaos"
x, y, iterations = getUserInput()
printTitles(x, y)
for i in range(1, iterations+1)
print '%10s %20s %20s' % (i, chaos(x), chaos(y))
raw_input("Press any key to exit")

def printTitles(x, y) :
# Prints the table borders and titles
print '%10s %20s %20s' % ("Index", x, y)
print "--"

def getUserInput() :
# Should put this inside a try/except block
x = float(raw_input("Enter a number between 1 and 0: "))
y = float(raw_input("Enter a second number between 1 and 0: "))
iterations = float(raw_input("Enter a number of iterations: "))

return x, y, iterations

def chaos(x) :
return (3.9 * x * (1 - x))


if __name__ == '__main__' :
main()

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


Re: [Tutor] Anyone fancy giving me some tips and an expert opinion??

2008-02-07 Thread Kent Johnson
Damian Archer wrote:
> # 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: ")

We generally discourage the use of input(), it is (arguably) a security 
hole -
http://www.wellho.net/mouth/956_Python-security-trouble-with-input.html
and a rather contentious discussion on this very list -
http://mail.python.org/pipermail/tutor/2007-August/056328.html
- and it doesn't validate the input. A better way to write this would be

 x = float(raw_input("Enter a number between 1 and 0: "))
 y = float(raw_input("Enter a second number between 1 and 0: "))

which is safer and guarantees that x and y are floating point numbers.

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


Re: [Tutor] Anyone fancy giving me some tips and an expert opinion??

2008-02-07 Thread Tiger12506
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


[Tutor] Anyone fancy giving me some tips and an expert opinion??

2008-02-07 Thread Damian Archer
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: ")
z = 0

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

# 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()

Thanks!!! And thanks for all the help you've all supplied me with so far,
you guys certainly are an extremely valuable resource!!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor