justifying text...and also...correct use of classes...

Ross La Haye rlahaye at new.rr.com
Wed Dec 29 21:39:37 EST 2004


Few questions.  I have a module "combin" which contains "Combin" and "Prob"
classes.  This is the "Prob" class:

class Prob:
    def binprob(self,n,k,p):
        fewerthan = sum([Combin().kcomb(n,i)*((p**i)*(1-p)**(n-i)) for i in
range(k)])
        exact = Combin().kcomb(n,k)*((p**k)*(1-p)**(n-k))
        notexact = 1 - exact
        atleast = 1 - fewerthan
        atmost = fewerthan + exact
        morethan = 1 - atmost

        T0 = (('Fewer Than:', fewerthan),
                ('At Least:',      atleast),
                ('Exactly:',       exact),
                ('Not Exactly:', notexact),
                ('At Most:',      atmost),
                ('More Than:',   morethan))

        for t in T0:
            print t[0].rjust(12),'\t',t[1]

Is the way I've used the "Combin" class considered correct/appropriate in
the "binprob" method?  If not, what is the standard usage?  

Also, I'm trying to get my results to print close to this(.i.e., I would
like the first column to be right justified and the second column to be
exactly as it is):

Fewer Than: 	0.376953125
    At Least: 	0.623046875
     Exactly: 	0.24609375
Not Exactly: 	0.75390625
     At Most: 	0.623046875
  More Than: 	0.376953125

but it comes out like this:

Fewer Than: 	0.376953125
   At Least: 	0.623046875
    Exactly: 	0.24609375
Not Exactly: 	0.75390625
    At Most: 	0.623046875
  More Than: 	0.376953125

I feel like I'm missing something obvious here and apologize in advance if I
am, but I just can't figure it out.  Any help would be appreciated.  BTW,
I'm not using a dictionary because I want the results to print exactly in
the order specified and that seemed the simplest way.  Also, I used
rjust(12) because len("Not Exactly:") = 12 etc.
     
hawk


-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 2356 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20041229/bd6bbd8a/attachment.bin>


More information about the Python-list mailing list