[Tutor] my first project: a multiplication trainer

2008-03-14 Thread Guba
Hello everybody,

I want to create a simple multiplication trainer which quizzes me on the
multiplication table. All multiplication combinations should be asked
once, without repetition.

Here my pseudo code:

###

A = [1,2,3,4,5,6,7,8,9]
B = [1,2,3,4,5,6,7,8,9]

asked_questions = ()
false_answers = ()

#block 1
While tuple "asked_questions" contains less than 81 items:
Take one random item from A and one random item from B
If the question is NOT in "asked_questions":
Add question to tuple "asked_questions" 
Calculate the question and wait for user-input  
print the question # e.g.: 3x6=
If answer is false:
add question to the tuple "false_answers"
print: "You have answered all questions!"   

###


I would very much appreciate if you could comment on/criticise my pseudo
code. In particular: is my approach correct, or would it be more
sensible to first generate (or supply?) all possible questions and then
select the ready questions randomly?

So far I am not concerned about what to do with the tuple
"false_answers". This I worry about later.

Thanks for your help!

Guba


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


Re: [Tutor] Working with Python Objects

2008-03-14 Thread Alan Gauld
"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote

class A:
constantA = 9
def OneOfA:

a = 

class B:
variableB = "quick brown fox"
def OneOfB:

b = 
c = b * a# the 'a' from def OneOfA in class A
--
> Question:
> 1) how do I access the 'a' from function (method) OneOfA in
> class A so that it can be used by functions (methods) in class B?

You don't and shouldn't try to. In this case because the attriute
only exists inside the method, it is local, so dies when the
method completes. So first of all you need to make it part
of the class A. We do that by tagging it as an attribute of
self, which should be the fitrst attribute of every method.

But one of the concepts of OOP is to think in terms of the
objects not the attributes inside them So your question
should probably be: How do I access objects of class A
inside methods of class B?

The answer is by passing an instance into the method as a
parameter. You can then manipulate the instance of A by
sending messages to it. In Python you can access the
instance values of an object by sending a message with
the same name as the attribute - in other OOP languages
you would need to provide an accessor method.

But it is very important conceptually that you try to get away
from thinking about accessing attributes of another object
inside methods. Access the objects. Metthods should only
be manipulating the attributes of their own class. To do
otherwise is to break the reusability of your classes.

So re writing your pseudo code:

class A:
constantA = 9
def OneOfA(self):   # add self as first parameter

self.a =# use 'self' to tag 'a' as 
an attribute

class B:
variableB = "quick brown fox"
def OneOfB(self, anA):# add self and the instance of A

b = 
c = b * anA.a# the 'a' from the instance anA

This way OneOfB() only works with attributes local to it
or defined as instance variables or passed in as arguments.
Which is as it should be!

Real OOP purists don't like direct attribute access but
in Python its an accepted idiom and frankly there is little
value in writing an accessor method that simply returns
the value if you can access it directly. The thing you
really should try to avoid though is modifying the attributes
directly from another class. Normally you can write a
more meaningful method that will do that for you.

-- 
Alan Gauld
Author of the Learn to Program web site
Temorarily at:
http://uk.geocities.com/[EMAIL PROTECTED]/
Normally:
http://www.freenetpages.co.uk/hp/alan.gauld 


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


[Tutor] My tutor is back

2008-03-14 Thread ALAN GAULD
I have no idea why it took my ISP so long but after 4 weeks 
my Tutorial is finally back up on its original address...

Hooray!

Alan G.
http://www.freenetpages.co.uk/hp/alan.gauld/


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


Re: [Tutor] Working with Python Objects

2008-03-14 Thread Greg Graham
Dinesh wrote:
> I've avoided it as long as possible but I've reached a stage where I
have to
> start using Python objects!  The primary reason is that the web
framework uses
> objects and the second is to eliminate a few globals.  Here is example
pseudo
> code followed by the question (one of many I suspect!):
>
> class A:
> constantA = 9
> def OneOfA:
> 
> a = 
>
> class B:
> variableB = "quick brown fox"
> def OneOfB:
> 
> b = 
> c = b * a# the 'a' from def OneOfA in class A
>
> Question:
> 1) how do I access the 'a' from function (method) OneOfA in class A so
that
> it can be used by functions (methods) in class B?


I wrote the following classes as an example that might be what you want.

 1  class A(object):
 2   def one_of_a(self):  # self is required for a method
 3   self.a = 3
 4   print self.a
 5
 6   class B(object):
 7   def one_of_b(self, a_obj):
 8   b = 10
 9   self.c = b * a_obj.a
10   print self.c

First of all, note that the method definitions have a "self" argument.
This
is how the method has access to the object for which they are called. If
you look on line 3, I put "self.a" so that the 3 is stored in the "a"
field of the object.

Since you wanted to access the a field of an A object in your one_of_b
method,
it will need access to an A object, so I added a parameter that you can
use
to pass in an A object (line 7). Assuming an A object is passed in, then
the
a field of it can be accessed as in line 9, "a_obj.a".

Here is how I ran the code (using IPython):

In [17]: v1 = A()

In [18]: v1.one_of_a()
3

In [19]: v2 = B()

In [20]: v2.one_of_b(v1)
30

At [17], I create an instance of A and assign it to variable v1.
At [18] I call the one_of_a method on my newly created object, which
prints
out a 3, the value stored in the "a" field of v1 object.
At [19], I create an instance of B and assign it to variable v2.
At [20], I call the one_of_b method on my new object, passing in the A
object assigned to v1. It prints out 30, which is 10 times the value
in the "a" field of v1.

The class, variable, and method names are pretty abstract and similar,
so I
hope that doesn't cause undue confusion. It might be easier to
understand
with a more concrete example.


Greg

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


Re: [Tutor] hypotenuse

2008-03-14 Thread bob gailer
Robert Childers wrote:
> I have rewritten my "hypotenuse" program as follows:>>> #This program 
> calculates the width and diagonal of a golden rectangle
> >>> print "Calculate the width and diagonal of a golden rectangle."
> Calculate the width and diagonal of a golden rectangle.
> >>> height = input ("Input height:")
> Input height:1
> >>> width = height*1.618
> >>> print "Width:", width
> Width: 1.618
> >>> import math
> >>> hyp_squared = height**2 + width**2
> >>> hypotenuse = math.sqrt(hyp_squared)
> >>> print "Diagonal:", hypotenuse
> Diagonal: 1.90208412012
>  
> When I save the program then try to run the module I get an error 
> message that it is invalid.

Please ALWAYS post the code and the traceback. Otherwise we have no way 
to easily help you.

But I will take a guess that you saved the contents of the interactive 
session and tried to run that. That will not work, as  the interactive 
session is full of >>> and results of print statements.

So I suggest you edit the module to see if this is the case; if it is 
then remove all the junk so you just have pure Python. Run that. If you 
still get errors post the code and traceback.

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

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


[Tutor] pydoc introspecting info via OptionParser?

2008-03-14 Thread Neal McBurnett
I'm trying to do nice clean documentation for a python script I run
from the command-line, and I'd like pydoc or a similar program to
document it well.  But I don't want to duplicate option information by
putting it both in my docstring and in optparse.

I would think that pydoc might notice an OptionParser instance at the
module level and show the options defined there, but it doesn't seem
to.  Would it be hard to add to pydoc?  Do any other documentation
programs do that?

I know that on aspn there is a library to parse a docstring and pass
options to optparse:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278844

But it has limitations (doesn't support all of optparse, won't work
with -OO) and is a bit uglier.

Cheers,

Neal McBurnett http://mcburnett.org/neal/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Working with Python Objects

2008-03-14 Thread Dinesh B Vadhia
I've avoided it as long as possible but I've reached a stage where I have to 
start using Python objects!  The primary reason is that the web framework uses 
objects and the second is to eliminate a few globals.  Here is example pseudo 
code followed by the question (one of many I suspect!):

class A:
constantA = 9
def OneOfA:

a = 

class B:
variableB = "quick brown fox"
def OneOfB:

b = 
c = b * a# the 'a' from def OneOfA in class A

Question:
1) how do I access the 'a' from function (method) OneOfA in class A so that it 
can be used by functions (methods) in class B?

Cheers

Dinesh


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


Re: [Tutor] hypotenuse

2008-03-14 Thread Robert Childers
I have rewritten my "hypotenuse" program as follows:>>> #This program
calculates the width and diagonal of a golden rectangle
>>> print "Calculate the width and diagonal of a golden rectangle."
Calculate the width and diagonal of a golden rectangle.
>>> height = input ("Input height:")
Input height:1
>>> width = height*1.618
>>> print "Width:", width
Width: 1.618
>>> import math
>>> hyp_squared = height**2 + width**2
>>> hypotenuse = math.sqrt(hyp_squared)
>>> print "Diagonal:", hypotenuse
Diagonal: 1.90208412012

When I save the program then try to run the module I get an error message
that it is invalid.
I have been assuming that if no "invalid syntax" error occurs in the shell
the program should run.  Could this problem be associated with the import
math instruction?

On Fri, Mar 14, 2008 at 6:03 AM, Alan Gauld <[EMAIL PROTECTED]>
wrote:

> "Wolfram Kraus" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > Am 14.03.2008 09:40, Alan Gauld schrieb:
>  Why can you not use something like:
> 
> >>> hypotenuse = hyp_squared**1/2
> >>
> >> And for completeness that could also be written:
> >>
> >> hypotenuse = pow(hyp_squared,1/2)
> >>
> >> Again, without the need to import math.
> >>
> > But beware of the integer divison in Python:
> >
> > >>> pow(2,1/2)
> > 1
>
> Good catch. Applies to the ** style too.
>
> Alan G
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hypotenuse

2008-03-14 Thread Alan Gauld
"Wolfram Kraus" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Am 14.03.2008 09:40, Alan Gauld schrieb:
 Why can you not use something like:

>>> hypotenuse = hyp_squared**1/2
>>
>> And for completeness that could also be written:
>>
>> hypotenuse = pow(hyp_squared,1/2)
>>
>> Again, without the need to import math.
>>
> But beware of the integer divison in Python:
>
> >>> pow(2,1/2)
> 1

Good catch. Applies to the ** style too.

Alan G 


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


Re: [Tutor] Idle

2008-03-14 Thread Alan Gauld
"Michael Connors" <[EMAIL PROTECTED]> wrote 

> You need to put :
> import random
> 
> or alternatively use:
> from random import *

Or better still use 

from random import randrange

import * style is not recommended because of the dangers 
of polluting the namespace


-- 
Alan Gauld
Author of the Learn to Program web site
Temorarily at:
http://uk.geocities.com/[EMAIL PROTECTED]
Normally:
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Idle

2008-03-14 Thread Bartruff, Pamela J.
I'm very new to Python, but it seems like I just learned in a class for
a random number, you would need the :
import random

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Christopher Marlett
Sent: Thursday, March 13, 2008 7:44 PM
To: tutor@python.org
Subject: [Tutor] Idle

Hi, I'm trying to write a program that uses the range function to
produce and then print [1, 2, 3, 4]. then prompt the user for an integer
n and print the list [1, 2, 3, ... , n] - including n. Then use a simple
repeat loop to print five randomly chosen numbers from the range [1, 2,
3, ... , n]

This is what I have done so far
print [1,2,3,4]
nstring = raw_input("Enter any number: ")
n = int(nstring)
print range(1,n+1)
y = random.randrange(1,n)
print y

It all works up until I need to print five randomly chosen numbers from
the range [1,n]. It tells me that random is not defined.

Thank you for your time.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Idle

2008-03-14 Thread Michael Connors
>
> It all works up until I need to print five randomly chosen numbers from
> the range [1,n]. It tells me that random is not defined.


You need to put :
import random

in your program before doing: y = random.randrange(1,n)

or alternatively use:
from random import *

and call the function as follows:
y = randrange(1,n)

Regards,

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


[Tutor] Idle

2008-03-14 Thread Christopher Marlett
Hi, I'm trying to write a program that uses the range function to
produce and then print [1, 2, 3, 4]. then prompt the user for an integer
n and print the list [1, 2, 3, ... , n] – including n. Then use a simple
repeat loop to print five randomly chosen numbers from the range [1, 2,
3, ... , n]

This is what I have done so far
print [1,2,3,4]
nstring = raw_input("Enter any number: ")
n = int(nstring)
print range(1,n+1)
y = random.randrange(1,n)
print y

It all works up until I need to print five randomly chosen numbers from
the range [1,n]. It tells me that random is not defined.

Thank you for your time.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] hypotenuse

2008-03-14 Thread Wolfram Kraus
Am 14.03.2008 09:40, Alan Gauld schrieb:
>>> Why can you not use something like:
>>>
>> hypotenuse = hyp_squared**1/2
> 
> And for completeness that could also be written:
> 
> hypotenuse = pow(hyp_squared,1/2)
> 
> Again, without the need to import math.
> 
But beware of the integer divison in Python:

 >>> pow(2,1/2)
1
 >>> pow(2,.5)
1.4142135623730951
 >>> pow(2,1.0/2)
1.4142135623730951

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


Re: [Tutor] hypotenuse

2008-03-14 Thread Alan Gauld
>> Why can you not use something like:
>>
>> >>>hypotenuse = hyp_squared**1/2

And for completeness that could also be written:

hypotenuse = pow(hyp_squared,1/2)

Again, without the need to import math.

-- 
Alan Gauld
Author of the Learn to Program web site
Temorarily at:
http://uk.geocities.com/[EMAIL PROTECTED]/
Normally:
http://www.freenetpages.co.uk/hp/alan.gauld

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