所以,這裏是我的Shoping Cart代碼,我堅持使用這個代碼,我修正了一個錯誤,另一個彈出。所以,現在我得到類型錯誤:getTotal()到底需要2個參數(1給出)getTotal()只需要2個參數(1給出)
class Item():
"""Name and price of Item"""
def __init__(self, name, price):
self.name = name
self.price = price
def getName(self): #Returning item's name
return self.name
def getPrice(self): #Returning item's price.
return self.price
class User():
"""Getting name of user"""
def __init__(self, name,budget):
self.name = name
self.budget = budget
def userName(self): #Returning user's name
return self.name
def userBudget(self): #Returning user's budget
return self.budget
class Cart():
"""Creating a cart, you can add item in cart and remove it, also u can se your total bill."""
def __init__(self):
self.carta = [] #Carta is shopping cart.
def addItem(self,carta):
self.carta.append(1) #Adding item in cart.
def getTotal(self,carta): #Total bill.
total = 0
for item in carta:
item = getPrice, getName
total += item
return total
def numItems(self,carta): #Number of items in cart.
self.carta = carta.len()
return len.carta()
def kart():
item1 = Item ("Iphone", 500)
item2 = Item ("Samsung", 200)
item3 = Item("Huawei", 400)
uname = User("Marko", 2000)
kart = Cart()
kart.addItem(item1)
kart.addItem(item2)
kart.addItem(item3)
print ("Hi %i, your total bill is $%0.2f, and you have %i items in your cart.",uname.userName(), kart.getTotal(), kart.numItems())
final = kart()
print (final)
輸出I得到:
Traceback (most recent call last):
File "C:\Users\Marko\Documents\Projects\Shopping.py", line 56, in <module>
final = kart()
File "C:\Users\Marko\Documents\Projects\Shopping.py", line 54, in kart
print ("Hi %i, your total bill is $%0.2f, and you have %i items in your cart.",uname.userName(), kart.getTotal(), kart.numItems())
TypeError: getTotal() takes exactly 2 arguments (1 given)
每一個尖,每一個幫助是值得歡迎,感謝,
該錯誤告訴你到底發生了什麼問題,以及在哪裏。什麼讓你對這個信息感到困惑? –
'kart.getTotal()' - 看起來像帶_zero_參數的調用... – CBroe
@CBroe'self'參數是隱式的。不要加入混亂。 – Boldewyn