我正在運行一個簡單的在線購物車程序,當我試圖運行它時,最終結果爲空。我理解關於類和對象的概念,但我真的需要幫助。它應該是這樣的:Python中的類和對象
Item 1
Enter the item name: Chocolate Chips
Enter the item price: 3
Enter the item quantity: 1
Item 2
Enter the item name: Bottled Water
Enter the item price: 1
Enter the item quantity: 10
TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10
Total: $13
這裏是我到目前爲止寫的,:
class ItemsToPurchase :
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):
total = item_quantity * item_price
print('%s %d @ $%f = $%f' % (item_name, item_quantity, item_price, total))
def main():
print('Item 1')
print()
item_name = str(input('Enter the item name: '))
item_price = float(input('Enter the item price: '))
item_quantity = int(input('Enter the item quantity: '))
item_one = ItemsToPurchase(item_name, item_price, item_quantity)
item_one.print_item_cost()
print('Item 2')
print()
item_name = str(input('Enter the item name: '))
item_price = float(input('Enter the item price: '))
item_quantity = int(input('Enter the item quantity: '))
item_two = ItemsToPurchase(item_name, item_price, item_quantity)
item_two.print_item_cost()
print('TOTAL COST')
item_one.print_item_cost()
item_two.print_item_cost()
if __name__ == "__main__":
main()
我做了什麼錯?
不知道這僅僅是你問的問題但是,對main()的調用不會在if語句 – Sighonide
下縮進。您得到的錯誤是什麼? – Prajwal
這是在一個單獨的過程中運行嗎? – Sighonide