我是該站點的新手,并且正在尋找代碼審查,因為此代碼“有效”,但該方法有一個多余的列印陳述句。輸出格式正確,但列印陳述句可能完成了大部分作業,而不是代碼?每次必須考慮新實體時都必須更改方法是否正常?如果我只是添加了一個新實體(沒有鏡像方法中的更改),則“總計:”輸出在此代碼中將無法正常作業。給出的示例輸入是 10 瓶水,每瓶 1 美元,一袋巧克力片,每瓶 3 美元。
class ItemToPurchase:
def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
def print_item_cost(self, item_name):
print('{} {} @ ${} = ${}'.format(item_name, item_quantity, item_price, (item_price * item_quantity)))
print('{} {} @ ${} = ${}'.format(item_name2, item_quantity2, item_price2, (item_price2 * item_quantity2)))
if __name__ == "__main__":
# Type main section of code here
item_one = ItemToPurchase()
print('Item 1')
item_name = input('Enter the item name:\n')
item_price = int(input('Enter the item price:\n'))
item_quantity = int(input('Enter the item quantity:\n'))
item_two = ItemToPurchase(item_name, item_price, item_quantity)
print()
print('Item 2')
item_name2 = input('Enter the item name:\n')
item_price2 = int(input('Enter the item price:\n'))
item_quantity2 = int(input('Enter the item quantity:\n'))
print()
print('TOTAL COST')
item = ItemToPurchase()
item.print_item_cost(item_name)
print()
print('Total: $' str((item_price * item_quantity) (item_price2 * item_quantity2)))
uj5u.com熱心網友回復:
該引數是該類的任何self實體的替身。它包含有關由該方法實體化的物件的給定實體的所有資訊。該引數僅對類的單個實體可見 - 因此兩者都可以參考并且只能獲取有關它們自己的資訊。__init__selfitem_1item_2self
為了列印您的物件,我建議__str__您改為實作,它允許您簡單地呼叫print(instance_of_my_class):
class ItemToPurchase:
def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
self.total_cost = self.item_price * self.item_quantity
def __str__(self):
return f"{self.item_name} {self.item_quantity} @ {self.item_price:.2f} = ${self.total_cost:.2f}"
請注意使用 f 字串,這使得閱讀和理解格式化字串應該做什么變得更容易一些。.2f還要注意價格和新屬性的格式代碼的使用self.total_cost(這將正確顯示您的價格 - 例如,而不是$21.0它會回傳$21.00)。
用法:
item = ItemToPurchase('ping pong paddle', 5.25, 4)
print(item)
輸出:
ping pong paddle 4 @ 5.25 = $21.00
uj5u.com熱心網友回復:
您似乎誤解了課程的目的。您的類的每個實體都代表該類的一個物件,其方法應該只關心該物件。所以print_item_cost應該只列印自己的成本,它應該從self引數中獲取它需要的所有資訊,引數表示方法所屬的物件。
class ItemToPurchase:
def __init__(self, item_name = 'none', item_price = 0, item_quantity = 0):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
def print_item_cost(self):
print('{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, self.item_price, (self.item_price * self.item_quantity)))
請注意,print_item_cost它不需要item_name引數,因為它從self.
然后,您可以創建任意數量的類實體并列印每個實體的資訊:
if __name__ == "__main__":
print('Item 1')
item_name = input('Enter the item name:\n')
item_price = int(input('Enter the item price:\n'))
item_quantity = int(input('Enter the item quantity:\n'))
# Create the item using the inputs you took
item_one = ItemToPurchase(item_name, item_price, item_quantity)
print()
print('Item 2')
item_name2 = input('Enter the item name:\n')
item_price2 = int(input('Enter the item price:\n'))
item_quantity2 = int(input('Enter the item quantity:\n'))
item_two = ItemToPurchase(item_name2, item_price2, item_quantity2)
item_one.print_item_cost()
item_two.print_item_cost()
理想情況下,您應該回圈使用它并使用串列來跟蹤專案,但這是一個不同的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464527.html
標籤:Python python-3.x
