oop - How would I make a loop that creates instances in python 3? -


i've looked @ questions creating instances in loop need create new instances different values each time, not bunch of clones of instance.

below, have written restaurant tip calculator (with tax) instances of class bill. wrote class method creates new instance when calculating first instance complete.

while works if have .py file open keep adding new instances , calling class methods needed, far more useful if somehow created loop create new instance when type "yes" in self.choice of second class method.

my previous attempts make loop resulted in creating unnamed instances. "return bill(example, example, example)" doesn't me because not able call methods on (i give headaches , not pythonic.) nor able append list.

persons = []  class bill:     def __init__(self, check, tax, tip):         self.check = check         self.tax = tax         self.tip = tip      def addpersons(self):         self.choice = input("do want calculate person?")         if self.choice == "yes" or self.choice == "yes":             person2 = bill(float(input("check: ")), float(input("tax: ")), float(input("tip: ")))             return person2         else:             pass      def percent(self):         self.tax = self.tax/100         self.tip = self.tip/100      def calculate(self):         self.result_1 = self.check + (self.check * self.tax)         self.result_final = self.result_1 + (self.result_1 * self.tip)         self.difference = self.result_final - self.result_1         self.advice = self.result_1, "is check tax added.", self.difference, "is how tip need pay.", self.result_final, "is total."         return self.advice  = bill(float(input("check: ")), float(input("tax: ")), float(input("tip: "))) a.percent() a.calculate() print(a.advice) persons.append(a)  b = a.addpersons() b.percent() b.calculate() print(b.advice) persons.append(b)  c = b.addpersons() c.percent() c.calculate() print(c.advice) persons.append(c) 

thank time , help. :)

i refactor addpersons() method out of class , things what's shown below. note made calculate() automatically call percent() doesn't have done externally.

this better design because moves responsibility interacting user , getting input outside of class (which aren't concern). allows used different user interfaces or programmatically, data in database or other container.

class bill:     def __init__(self, check, tax, tip):         self.check = check         self.tax = tax         self.tip = tip      def percent(self):         self.tax = self.tax/100         self.tip = self.tip/100      def calculate(self):         self.percent()         self.result_1 = self.check + (self.check * self.tax)         self.result_final = self.result_1 + (self.result_1 * self.tip)         self.difference = self.result_final - self.result_1         self.advice = (self.result_1, "is check tax added.",                        self.difference, "is how tip need pay.",                        self.result_final, "is total.")         return self.advice  bills = [] while true:     choice = input("do want calculate person?")     if choice.lower().startswith("y"):         break     bill = bill(float(input("check: ")), float(input("tax: ")),                 float(input("tip: ")))     bill.calculate()     print(*bill.advice)     bills.append(bill) 

the loop not create named instances of bill class. instead stores them in list called bills. if wanted associate person's name each one, instead put them in dictionary keyed name.

bills = {} while true:     choice = input("do want calculate person?")     if choice.lower().startswith("y"):         break     person_name = input("enter name of person: ")     if not person_name:         continue  # ask continuing again     bill = bill(float(input("check: ")), float(input("tax: ")),                 float(input("tip: ")))     bill.calculate()     print("{}'s bill:".format(person_name))     print(*bill.advice)     bills[person_name] = bill 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -