所以,这是我的购物车代码,我坚持使用此代码,我修复了一个错误,另一个弹出。所以现在我得到TypeError: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)
输出我得到:
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)
每一个提示,每一个帮助都是受欢迎的,谢谢,
答案 0 :(得分:0)
你班上有几个错误。
首先,在Python中,您可以摆脱getXXX()
方法,尤其是在您的情况下。因此,您可以将Item
和User
类重新定义为:
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
也无法正常工作(!)。如果您想查找carta 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 :(得分: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)
对。
答案 2 :(得分:-1)
好吧,在你的打印中你打电话给kart.getTotal()
但是在购物车的定义中,你有这个方法def getTotal(self,carta)
。
因此,当你调用它时,你必须传递2个参数。
你只传递一个kart
。因此,如果我正确理解您要尝试做什么,则必须将方法getTotal更改为def getTotal(self)
,因为您不使用参数carta