2017-07-17 50 views
-5

所以,這裏是我的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) 

每一個尖,每一個幫助是值得歡迎,感謝,

+4

該錯誤告訴你到底發生了什麼問題,以及在哪裏。什麼讓你對這個信息感到困惑? –

+1

'kart.getTotal()' - 看起來像帶_zero_參數的調用... – CBroe

+3

@CBroe'self'參數是隱式的。不要加入混亂。 – Boldewyn

回答

0

你的類有幾個錯誤。

首先,在Python中,你可以刪除getXXX()方法,特別是在你的情況。所以,你可以重新定義ItemUser類爲:

class Item(): 
    """Name and price of Item""" 
    def __init__(self, name, price): 
     self.name = name 
     self.price = price 

class User(): 
    """Getting name of user""" 
    def __init__(self, name,budget): 
     self.name = name 
     self.budget = budget 

Cart類,代碼不你想要什麼:

def addItem(self,carta): 
    self.carta.append(1) #Adding item in cart. 

它將整數1追加到當前cart。這樣做,你會,考慮到當前的項目補充:

def addItem(self, item): 
    self.carta.append(item) 

接下來,getTotal不能工作,因爲它是:

def getTotal(self, carta): # carta is not needed 
    total = 0 
    for item in carta: # work with the current carta, so use self.carta 
     item = getPrice, getName # I guess you want to retrieve the item's price, so use item.price 
     total += item 
    return total 

最後,numItems不能工作過(!)。如果你想找到憲章length(如carta是一個列表),這樣做:

def numItems(self): #Number of items in cart. 
    return len(self.carta) 

最終代碼修復和右print聲明:

class Item(): 
    """Name and price of Item""" 
    def __init__(self, name, price): 
     self.name = name 
     self.price = price 

class User(): 
    """Getting name of user""" 
    def __init__(self, name,budget): 
     self.name = name 
     self.budget = 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, item): 
     self.carta.append(item) #Adding item in cart. 

    def getTotal(self): #Total bill. 
     total = 0 
     for item in self.carta: 
      total += item.price 
     return total 

    def numItems(self): #Number of items in cart. 
     return len(self.carta) 

def test_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 %s, your total bill is $%0.2f, and you have %i items in your cart.' % (uname.name, kart.getTotal(), kart.numItems())) 

test_kart() 

我們可以做一個很多pythonic,但它似乎你正在學習Python,所以嘗試正確理解該片段,然後嘗試優化,如果需要和需要。並閱讀that tutorial :)

-1

好,在您的打印中您撥打kart.getTotal()但在購物車的定義中,您有此方法def getTotal(self,carta)。 所以當你調用它時,你必須傳遞2個參數。

您只能通過一個是kart。所以,如果我理解你正在嘗試做什麼,你必須改變方法getTotal到def getTotal(self),因爲你不使用參數carta

0

Cart類已經包裝在購物車中的物品清單。您無需將任何參數傳遞給getTotal。電話是正確的;定義不是。

addItem確實需要一個參數,但它是要添加到購物車的項目,而不是購物車本身。您當前的實施僅在每次調用時將數字1添加到購物車,而不是第一個參數。

同樣地,numItems只需要返回self.carta的長度,不要接受任何額外的參數。

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 = [] 

    def add_item(self, item): 
     self.carta.append(item) 

    def get_total(self): 
     total = 0 
     for item in self.carta: 
      total += item.price 
     return total 

    def num_items(self): 
     return len(self.carta) 

Python不是Java。你不需要爲簡單的屬性訪問定義單獨的函數(正如我上面定義的get_total暗示的那樣,直接訪問item.price)。

class Item: 
"""Name and price of Item""" 
    def __init__(self, name, price): 
     self.name = name 
     self.price = price 

class User: 
"""Getting name of user""" 
    def __init__(self, name,budget): 
     self.name = name 
     self.budget = budget 

最後,你想要麼kart(不打印)的字符串,或與final=.../print(final)對免除。

相關問題