[Tutor] help

2009-04-14 Thread j mercedes

hi, its me again jay, thanks alot everyone for your input on the last program i 
created that solved for the mode,mean, and average of a set of numbers it 
actually worked great..if anyone can give me any ideas on this program i am 
doing now it would be appreciated.I keep would like to understand why on the 
last portion it returns an error.thank you.import shelveimport stringUNKNOWN = 
0HOME = 1WORK = 2FAX = 3CELL = 4class phoneentry:def __init__(self, name = 
'Unknown', number = 'Unknown',type = UNKNOWN):self.name = name
self.number = numberself.type = type#  create string representationdef 
__repr__(self):return('%s:%d' % ( self.name, self.type ))#  fuzzy compare 
or two itemsdef __cmp__(self, that):this = string.lower(str(self))that 
= string.lower(that)if string.find(this, that) = 0:return(0)   
 return(cmp(this, that))def showtype(self):if self.type == UNKNOWN: 
return('Unknown')if self.type == HOME: return('Home')if self.type == 
WORK: return('Work')if self.type == FAX: return('Fax')if self.type == 
CELL: return('Cellular')class phonedb:def __init__(self, dbname = 
'phonedata'):self.dbname = dbname;self.shelve = 
shelve.open(self.dbname);def __del__(self):self.shelve.close()
self.shelve = Nonedef add(self, name, number, type = HOME):e = 
phoneentry(name, number, type)self.shelve[str(e)] = edef lookup(self, 
string):list = []for key in self.shelve.keys():e = 
self.shelve[key]if cmp(e, string) == 0:list.append(e)
return(list)
if __name__ == '__main__':foo = phonedb()foo._add_('Sean 
Reifschneider', '970-555-', HOME)foo.add('Sean Reifschneider', 
'970-555-', CELL)foo.add('Evelyn Mitchell', '970-555-', HOME)print 
'First lookup:'for entry in foo.lookup('reifsch'):print '%-40s %s (%s)' 
% ( entry.name, entry.number, entry.showtype())print 'Second lookup:'for entry 
in foo.lookup('e'):print '%-40s %s (%s)' % ( entry.name, entry.number, 
entry.showtype() )___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] FW: another newbie

2009-04-12 Thread j mercedes










hi, its me again jay, thanks alot everyone for your input on the last program i 
created that solved for the mode,mean, and average of a set of numbers it 
actually worked great...i used the import in a bit different method but still 
very valuable...if anyone can give me any ideas on this program i am doing now 
it would definetly be appreciated here it goes:import shelveimport 
stringUNKNOWN = 0HOME = 1WORK = 2FAX = 3CELL = 4class phoneentry:def 
__init__(self, name = 'Unknown', number = 'Unknown',type = UNKNOWN):
self.name = nameself.number = numberself.type = type#  create 
string representationdef __repr__(self):return('%s:%d' % ( self.name, 
self.type ))#  fuzzy compare or two itemsdef __cmp__(self, that):this = 
string.lower(str(self))that = string.lower(that)if string.find(this, 
that) = 0:return(0)return(cmp(this, that))def showtype(self):  
  if self.type == UNKNOWN: return('Unknown')if self.type == HOME: 
return('Home')if self.type == WORK: return('Work')if self.type == FAX: 
return('Fax')if self.type == CELL: return('Cellular')class phonedb:def 
__init__(self, dbname = 'phonedata'):self.dbname = dbname;  
  self.shelve = shelve.open(self.dbname);def __del__(self):
self.shelve.close()self.shelve = Nonedef add(self, name, number, type = 
HOME):e = phoneentry(name, number, type)self.shelve[str(e)] = edef 
lookup(self, string):list = []for key in self.shelve.keys():e = 
self.shelve[key]if cmp(e, string) == 0:list.append(e)
return(list)#  if not being loaded as a module, run a small testif __name__ == 
'__main__':foo = phonedb()foo._add_('Sean Reifschneider', 
'970-555-', HOME)foo.add('Sean Reifschneider', '970-555-', CELL)
foo.add('Evelyn Mitchell', '970-555-', HOME)print 'First lookup:'for entry 
in foo.lookup('reifsch'):print '%-40s %s (%s)' % ( entry.name, 
entry.number, entry.showtype())print 'Second lookup:'for entry in 
foo.lookup('e'):print '%-40s %s (%s)' % ( entry.name, entry.number, 
entry.showtype() )___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] toggle through a list

2009-04-01 Thread j mercedes

Hi, I have problem here...I am writing a program but I am getting stucked at 
how to solve the mode of a series of numbers...any ideas???following is the 
program:def mode(data):num=0count=0mx=0mode=0for i in data: 
   if num!=data[i]:num=data[i]count=count+1
else :count=count+1if countmx:mx=countmode=num 
   print 'The mode is: ',modedef median(lst):x=len(data)y=x/2
print 'The median is: ',data[y]def getlst():num=[]while True:
s=raw_input('please enter a series of number to evaluate')if 
s=='':breaknum.append(eval(s))return numdef avg(y):   sum(y)   
print'the average is: ', sum/len(y)data=getlst()median(data)avg(data)___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor