[Tutor] Class Tips

David david at abbottdavid.com
Sun May 31 00:59:39 CEST 2009


I took this program that determines a fertilizer application rate;

#!/usr/bin/python

rate = float(raw_input("Enter rate i.e. (0.5) : "))
n = float(raw_input("Enter N from bag i.e. (.14) : "))
app = rate / n
area = 43.560
acre = input("Enter total acre's to be treated: ")
bag = input("Enter bag weight: ")

print "You should apply", app * area * acre / bag, "bags."

And converted it to class/object to learn how they work. Just looking 
for some pointers, if I did it correctly etc.

#!/usr/bin/python

class FertRate:
     def __init__(self, rate, nitrogen, acre, bag):
         self.area = 43.560
         self.app = rate / (nitrogen / 100.00)
         self.acre = acre
         self.bag = bag

     def result(self):
         result = self.app * self.area * self.acre / self.bag
         print 'You should apply %0.2f bags.' % result

def main():
     rate, nitrogen, acre, bag = get_inputs()
     frate = FertRate(rate, nitrogen, acre, bag)
     frate.result()

def get_inputs():
     rate = float(raw_input('Enter Rate e.q., (0.5): '))
     nitrogen = float(raw_input('Enter N From Bag e.q., (14): '))
     acre = int(raw_input("Enter Total Acre's To Be Treated: "))
     bag = int(raw_input('Enter Bag Weight: '))
     return rate, nitrogen, acre, bag

main()

thanks,
-david
-- 
Powered by Gentoo GNU/Linux
http://linuxcrazy.com


More information about the Tutor mailing list