[Tutor] random equation generator

2008-10-23 Thread i i
hi, can u tell me any good tutorial site for pygtk, any that contains detail
explanation of pygtk.
i want to develop a game,that will randomly generate the operands,and the
answer,a user have to chosse from the operators(+,-,*,/).
which function should i use to randomly generate equation.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Need help w/ a for loop

2008-10-23 Thread Monte Milanuk
Hello all,

New guy here, so go easy on me ;)

I'm starting to work my way through Python Programming by Zelle, and have hit a 
bit of a wall on one of the programming exercises in Chapter 3 (#15 if anyone 
has the book handy).  

What the question ask is:  Write a program that approimates the value of pi by 
summing the terms of this series: 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11+...  The 
program should ask the user for 'n', the number of terms to sum, and then 
output the sum of the first 'n' terms of this series.

Where I am running into problems is how to do the '-' & '+', depending on the 
value of 'n'.  i.e. if 'n' = 3, it's going to be a - & an +, if 'n' =5 its 
going to be -, +, -, +, etc.  How to make that work in terms of an algorithm is 
making my head hurt (and its so early in the book yet... ;) )

Here's what I have thus far:

#   approximate_pi.py
#   Approximates the value of 'pi' by summing the terms of a series.
#

import math

def main():
print "This program will approximate the value of pi"
print "to a degree determined by the user. "
print

#  get the value of n from the user
n = input("How many terms do you want me to sum?  ")
print

#  create a loop from 1 to n+1, odd)
for i in range(1,n + 1,2):
#  each term is '4/i' as it steps thru the loop starting with 1
x = 4 / i
#  not sure where to go from here


print
#  output the sum - convert it to a float just in case
print "The sum of the numbers you entered is", (float(sum))

#  calculate the difference between our approximation and Python's pi
diff = sum - math.pi

#  output the difference
print
print "The difference between your 'pi' & Python's pi is", diff, "."



main()


Any assistance or nudges in the right direction would be most appreciated.

Thanks,

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


Re: [Tutor] random equation generator

2008-10-23 Thread W W
www.google.com/search?q=pygtk+tutorial
www.google.com/search?q=python+random+generator
www.google.com/search?q=python+dict

If you would like to develop the game, you should first try to develop the
game, and then ask when you get stuck.

If you can't figure out how to get a user to guess between operands '+',
'-', '*', '/' then my guess is you are very new to programming, and this
question shows you are very new to the tutor list. We're happy to help you
find a solution when you get stuck, but we won't write your program for you.
If you're stuck at the planning phase it seems that you may be trying to
tackle a subject that's a little too deep, and you'll find a lot of
difficulty in learning pyGTK.

Here are some assignments for you. Successfully completing these will not
only show that you're willing to learn and take advice, but they will also
help you in your quest to write your game.

tip: search google for the functions you should use, if you don't already
know them. Using the keyword "python" before the functionality will help
narrow your results.

1) Write a progam that will generate 10 random numbers in a /range/ of 0-3,
and print each one out.
After you do this, allow the user to /input/ the number of numbers (i.e. 5
instead of 10)
sample output:
Random Numbers:
0
3
1
2
0
0
2
1
3
1

Please enter a number: 4
0
1
3
1

2) Write a program that allows the user to input a number 0-3, and
determines which operand to print.
Sample output:

Enter a number: 0
Operand: +

Enter a number: 1
Operand: -

Enter a number: 3
Operand: /

Enter a number: 2
Operand: *

Do these things and you'll be well on your way to writing your program!
(also, feel free to use http://pastebin.com to post your code to report on
your progress)

Good luck!
HTH,
Wayne

If and only if you get stuck should you read this!

Seriously! Stop reading here! :)

Tip: Once you have searched google and the python docs for raw_input, while
loop, dict, list, if else, random and read all the documentation you can
find, if you cannot figure out the solution, only then should you ask
another question, and only about the topic on which you can't figure out on
your own!

On Thu, Oct 23, 2008 at 5:12 AM, i i <[EMAIL PROTECTED]> wrote:

> hi, can u tell me any good tutorial site for pygtk, any that contains
> detail explanation of pygtk.
> i want to develop a game,that will randomly generate the operands,and the
> answer,a user have to chosse from the operators(+,-,*,/).
> which function should i use to randomly generate equation.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help w/ a for loop

2008-10-23 Thread W W
On Wed, Oct 22, 2008 at 11:09 PM, Monte Milanuk <[EMAIL PROTECTED]> wrote:

> Hello all,
>
> New guy here, so go easy on me ;)
>

Welcome to python and the tutor list!


> I'm starting to work my way through Python Programming by Zelle, and have
> hit a bit of a wall on one of the programming exercises in Chapter 3 (#15 if
> anyone has the book handy).
>
> What the question ask is:  Write a program that approimates the value of pi
> by summing the terms of this series: 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11+...
> The program should ask the user for 'n', the number of terms to sum, and
> then output the sum of the first 'n' terms of this series.
>
> Where I am running into problems is how to do the '-' & '+', depending on
> the value of 'n'.  i.e. if 'n' = 3, it's going to be a - & an +, if 'n' =5
> its going to be -, +, -, +, etc.  How to make that work in terms of an
> algorithm is making my head hurt (and its so early in the book yet... ;) )
>

Here's a suggestion: look at only the value of the denominator (as the top
is obviously constant at 4, for the sake of pattern searching you only need
to worry about the variable) -

1 - 3 + 5 - 7 + 9 - 11 + ...

Do you notice a pattern? (if it doesn't pop out, read on...)





1+2 = ?
1+4 = ?
1+6 = ?
1+8 = ?
1+10 = ?

If you want a fairly easy way to figure out whether it should be + or -,
take another look at the pattern.

If you need a hint, read past my name.

HTH,
Wayne

Hint: Think of the pattern in terms of relation to 2


WAIT!
Don't read beyond here if you want to discover the solution yourself!




2/2 = 1
4/2 = 2
6/2 = 3
8/2 = 4
etc.


> Here's what I have thus far:
>
> #   approximate_pi.py
> #   Approximates the value of 'pi' by summing the terms of a series.
> #
>
> import math
>
> def main():
> print "This program will approximate the value of pi"
> print "to a degree determined by the user. "
> print
>
> #  get the value of n from the user
> n = input("How many terms do you want me to sum?  ")
> print
>
> #  create a loop from 1 to n+1, odd)
> for i in range(1,n + 1,2):
> #  each term is '4/i' as it steps thru the loop starting with 1
> x = 4 / i
> #  not sure where to go from here
>
>
> print
> #  output the sum - convert it to a float just in case
> print "The sum of the numbers you entered is", (float(sum))
>
> #  calculate the difference between our approximation and Python's pi
> diff = sum - math.pi
>
> #  output the difference
> print
> print "The difference between your 'pi' & Python's pi is", diff, "."
>
>
>
> main()
>
>
> Any assistance or nudges in the right direction would be most appreciated.
>
> Thanks,
>
> Monte
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help w/ a for loop

2008-10-23 Thread Kent Johnson
On Thu, Oct 23, 2008 at 12:09 AM, Monte Milanuk <[EMAIL PROTECTED]> wrote:

> def main():
> print "This program will approximate the value of pi"
> print "to a degree determined by the user. "
> print
>
> #  get the value of n from the user
> n = input("How many terms do you want me to sum?  ")
> print
>
> #  create a loop from 1 to n+1, odd)

Here you should initialize a variable to hold the sum. 'sum' is not a
good name because it is the name of a built-in function. 'total' is
better.

> for i in range(1,n + 1,2):

Note that this loop has n/2 steps, not n.
> #  each term is '4/i' as it steps thru the loop starting with 1
> x = 4 / i

Do you know about integer vs float division? What is the difference
between 4/9 and 4.0/9?

> #  output the sum - convert it to a float just in case

If it isn't already a float you won't have a very good estimate of pi.

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


Re: [Tutor] Need help w/ a for loop

2008-10-23 Thread bob gailer




Monte Milanuk wrote:

  
  
  Hello all,
  
New guy here, so go easy on me ;)
  
  


We save the whips and chains for the more hardened questers. Newcomers
get the feathers.


  
  
I'm starting to work my way through Python Programming by Zelle, and
have hit a bit of a wall on one of the programming exercises in Chapter
3 (#15 if anyone has the book handy).  
  
What the question ask is:  Write a program that approimates the value
of pi by summing the terms of this series: 4/1 - 4/3 + 4/5 - 4/7 + 4/9
- 4/11+...  The program should ask the user for 'n', the number of
terms to sum, and then output the sum of the first 'n' terms of this
series.
  
Where I am running into problems is how to do the '-' & '+',
depending on the value of 'n'.  i.e. if 'n' = 3, it's going to be a -
& an +, if 'n' =5 its going to be -, +, -, +, etc.  How to make
that work in terms of an algorithm is making my head hurt (and its so
early in the book yet... ;) )
  
  


There are many ways to handle this. Others have given some hints.

The simplest IMHO is to set the range stride to 4 instead of 2 and then
use x += 4.0/i - 4.0/(i + 2). 

You could also use a multiplier (let's call it m) that alternates
between 1 and -1. Roughly:

x = 0
m = 1
for in in range...
  x += 4.0/i*m
  m =  -m

For more generality and anticipating more complex algorithms:

import operator
ops = (operator.add, operator.sub)
x = 0
m = 0
for in in range...
  x = ops[m](x, 4.0/i)
  m =  1-m

And just for the heck of it you could write 2 for loops, each with a
stride of 4. The first would just add all the fractions to be added and
the second would add all the fractions to be subtracted, then combine
them. Throwing in the sum function and generator expressions:
pi = sum(4.0/i for i in range(1, n*2, 4)) - sum(4.0/i for i in range(3,
n*2, 4))

Applying sum and generator expressions to my original solution you get:
pi = sum(4.0/i - 4.0/(i + 2) for i in range(1, 4*n, 4))

Ah I can go on can't I? A lot more than you asked for!

-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?



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


Re: [Tutor] Need help w/ a for loop

2008-10-23 Thread Richard Lovely
Why not throw in itertools.cycle while you're at it? ;-)

pi = sum(4. / (1+x) * itertools.cycle((1,-1)).next() for x in range(0,
4 * n, 2))

I'd also be so tempted just to call the file 'approximate' (read it
with extension...)

Let's also not forget about integer division...

2008/10/23 bob gailer <[EMAIL PROTECTED]>:
> Monte Milanuk wrote:
>
> Hello all,
>
> New guy here, so go easy on me ;)
>
> We save the whips and chains for the more hardened questers. Newcomers get
> the feathers.
>
>
> I'm starting to work my way through Python Programming by Zelle, and have
> hit a bit of a wall on one of the programming exercises in Chapter 3 (#15 if
> anyone has the book handy).
>
> What the question ask is:  Write a program that approimates the value of pi
> by summing the terms of this series: 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11+...
> The program should ask the user for 'n', the number of terms to sum, and
> then output the sum of the first 'n' terms of this series.
>
> Where I am running into problems is how to do the '-' & '+', depending on
> the value of 'n'.  i.e. if 'n' = 3, it's going to be a - & an +, if 'n' =5
> its going to be -, +, -, +, etc.  How to make that work in terms of an
> algorithm is making my head hurt (and its so early in the book yet... ;) )
>
> There are many ways to handle this. Others have given some hints.
>
> The simplest IMHO is to set the range stride to 4 instead of 2 and then use
> x += 4.0/i - 4.0/(i + 2).
>
> You could also use a multiplier (let's call it m) that alternates between 1
> and -1. Roughly:
>
> x = 0
> m = 1
> for in in range...
>   x += 4.0/i*m
>   m =  -m
>
> For more generality and anticipating more complex algorithms:
>
> import operator
> ops = (operator.add, operator.sub)
> x = 0
> m = 0
> for in in range...
>   x = ops[m](x, 4.0/i)
>   m =  1-m
>
> And just for the heck of it you could write 2 for loops, each with a stride
> of 4. The first would just add all the fractions to be added and the second
> would add all the fractions to be subtracted, then combine them. Throwing in
> the sum function and generator expressions:
> pi = sum(4.0/i for i in range(1, n*2, 4)) - sum(4.0/i for i in range(3, n*2,
> 4))
>
> Applying sum and generator expressions to my original solution you get:
> pi = sum(4.0/i - 4.0/(i + 2) for i in range(1, 4*n, 4))
>
> Ah I can go on can't I? A lot more than you asked for!
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
> When we take the time to be aware of our feelings and
> needs we have more satisfying interatctions with others.
> Nonviolent Communication provides tools for this awareness.
> As a coach and trainer I can assist you in learning this process.
> What is YOUR biggest relationship challenge?
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help w/ a for loop

2008-10-23 Thread Kent Johnson
On Thu, Oct 23, 2008 at 10:20 AM, bob gailer <[EMAIL PROTECTED]> wrote:

> import operator
> ops = (operator.add, operator.sub)
> x = 0
> m = 0
> for in in range...
>   x = ops[m](x, 4.0/i)
>   m =  1-m

itertools.cycle(ops) is handy here. Hmm, there is a cute one-line
solution (excluding import and input) that uses itertools.cycle() and
zip()...not sure if this is a homework problem so I won't post it
yet...

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