[英]How would I make a loop that creates instances in python 3?
我看過類似在循環中創建實例的問題,但是我每次都需要創建具有不同值的新實例,而不是一堆實例的克隆。
在下面,我為Bill類的實例編寫了一個餐廳小費計算器(含稅)。 我編寫了一個類方法,當第一個實例的計算完成時,該方法將創建一個新實例。
雖然如果我打開.py文件以繼續添加新實例並調用所有所需的類方法,這是可行的,但是如果我以某種方式創建了一個循環,當我在窗口中鍵入“ yes”時將創建一個新實例,則它將更加有用。第二類方法的self.choice。
我之前進行循環的嘗試導致創建了未命名的實例。 “ Return Bill(示例,示例,示例)”對我沒有幫助,因為我將無法調用該方法(我可以,但是可能會令人頭疼,並且不會使用pythonic。)我也無法將其附加到列表中。
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 you want to calculate for another 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 your check with tax added.", self.difference, "is how much tip you need to pay.", self.result_final, "is your total."
return self.advice
a = 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)
感謝您的時間和幫助。 :)
我將在類之外重構addPersons()
方法,並執行如下所示的操作。 注意,我還使calculate()
自動調用percent()
因此不必在外部完成。
這是一個更好的設計,因為它將與用戶進行交互並在類本身之外獲取輸入的責任(這實際上並不是它的關注點)。 它還允許它與不同的用戶界面一起使用或以編程方式(例如從數據庫或其他容器中的數據)使用。
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 your check with tax added.",
self.difference, "is how much tip you need to pay.",
self.result_final, "is your total.")
return self.advice
bills = []
while True:
choice = input("Do you want to calculate for another 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)
該循環不會創建Bill
類的命名實例。 而是將它們全部存儲在名為bills
的列表中。 如果您想將一個人的姓名與每個人的姓名相關聯,則可以將其放入以姓名作為關鍵字的字典中。
bills = {}
while True:
choice = input("Do you want to calculate for another person?")
if choice.lower().startswith("y"):
break
person_name = input("Enter the name of the person: ")
if not person_name:
continue # ask about 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.