Python Restaurant Tip Calculator -
so complete newbie coding , need help. trying create code tip calculator not quite sure going it. never in class taught teacher going out on whim this. there requirement have class i know going wrong way. appreciated. have far:
import decimal class tip: def __init__(self): self.tip=0 def __str__(self): return 'tip['+str(self.sub_total)+' * '+str(self.percentage)+']' def sub_total(): self.sub_total = input() def percentage(): self.percentage = input() def get_tip(percentage, sub_total): self.percentage = decimal.decimal(percentage) self.sub_total = decimal.decimal(sub_total) self.tip = ((sub_total * percentage) / 100) self.total = (sub_total + tip) return self.__str__() t = tip() print ("how bill?") t.sub_total() print ("what percentage should tip be?") t.percentage() print (t.get_tip())
everytime run this:
get_tip() takes 2 arguments (1 given)
like said, kindof making go , appreciated.
hoo boy, there's lot of things cover. i'll try all, start working code.
class bill(object): # note: not tip! def __init__(self, sub_total): self.sub_total = sub_total def calculate_tip(self, tip_pct): return self.sub_total * tip_pct def calculate_total(self, tip_pct): return self.sub_total * (1 + tip_pct)
this defines bill
object. if want use it, do:
subtotal = float(raw_input("bill subtotal: ")) b = bill(subtotal)
then tip do
tip_percentage = float(raw_input("tip percent (15% --> 0.15): ")) tip_amt = b.calculate_tip(tip_percentage)
and grand total:
grand_total = b.calculate_total(tip_percentage)
here's went wrong:
your original class, reference, looks like:
class tip: def __init__(self,sub_total,percentage): self.tip= (sub_total * percentage) def __str__(self): return 'tip['+str(sub_total)+' * '+str(percentage)+']' def sub_total(): sub_total = input() def percentage(): percentage = input() def get_tip(percentage, sub_total): percentage = decimal.decimal(percentage) sub_total = decimal.decimal(sub_total) tip = ((sub_total * percentage) / 100) total = (sub_total + tip) return tip(total)
we'll talk method method.
__init__
this 1 great! means create tip
object passing sub_total , percentage (as ratio 1) in constructor, t = tip(15.00, 0.15)
15% tip on $15. hooray!
__str__
only briefly going go one, other mention looks more __repr__
__str__
, should use (
/)
instead of [
/]
. remember sub_total
, percentage
aren't attributes of object -- they're arguments passed constructor. can't reference them here if don't save them in __init__
method.
sub_total
first of all, can't call since doesn't have self
argument. second of doesn't if could. sub_total = input()
gets user input, throws away.
percentage
see above
get_tip
you're missing self
argument, again, i'll talk if get_tip(self, percentage, sub_total)
. call using already-created tip
object (remember above when did t = tip(15, 0.15)
?) , calling arguments passed constructor. in other words:
t = tip(15, 0.15) # 15% tip on $15 t.get_tip(0.15, 15) # ...15% tip on $15, grand total...
it doesn't make whole lot of sense function. since half work done , saved in self.tip
. if pattern wanted use object, like:
class tip(object): def __init__(self, sub_total, tip_pct): self.sub_total = sub_total self.tip_pct = tip_pct # saving these attributes can use them later self.grand_total = self.sub_total * (1 + self.tip_pct) self.tip_amt = self.sub_total * self.tip_pct
but looks more series of functions me, needs saved object!
nota bene
remember classes here provide state code run in. in case, state can apply "how tip," suppose reverse of , like:
t = tip(0.15) t.calculate_total(amt_of_bill)
but seems silly me. maybe make factory function like:
def tip(tip_pct): def wrapped(bill_amt): return bill_amt * (1 + tip_pct) return wrapped
but that's little advanced you're at.
Comments
Post a Comment