我正在为我的决赛学习,这是我错过的一个测验问题。我需要getTotal方法的大部分帮助。我需要遍历列表,查找每个项目的价格,将价格添加到总计并返回总计。我在循环中挣扎,我不知道如何将第二项从列表中拉出来。[1] ??我尝试过很多方法而且感到很沮丧。
如果有人愿意帮助我,那会很棒。我还在学习并且是新手,所以对我很轻松,但我真的想学习它。它可能没有我想象的那么难,但我会等待一些输入。谢谢!
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
def getPrice(self):
return self.price
def getName(self):
return self.name
class Cart:
def __init__(self, list):
self.list = []
def addItem(self, item):
self.list.append(self.list)
def getTotal(self):
total = 0
for item in self.list:
name, price = item # or price = item[1]
total = total + price
def getNumItems(self):
count = 0
for c in range(self.list):
count = self.list + 1
return count
def removeItem(self, item)
#removes the item from the cart's item list
def main():
item1 = Item("Banana", .69)
item2 = Item("Eggs", 2.39)
item3 = Item("Donut", .99)
c = Cart()
c.addItem(item1)
c.addItem(item2)
c.addItem(item3)
print "You have %i items in your cart for a total of $%.02f" %(c.getNumItems(), c.getTotal())
c.removeItem(item3)
print "You have %i items in your cart for a total of $%.02f" % (c.getNumItems(), c.getTotal())
main()
答案 0 :(得分:1)
for getTotal:
def getTotal(self):
total = 0
for item in self.list:
name, price = item # or price = item[1]
total = total + price
BTW,您的addItem和getNumItems方法也是错误的。由于它是最终的,你应该试着了解你正在做什么。
答案 1 :(得分:1)
这里给出了时间,我更改了代码,现在它是功能齐全的购物车
class Item(object):
def __init__(self, unq_id, name, price, qty):
self.unq_id = unq_id
self.product_name = name
self.price = price
self.qty = qty
class Cart(object):
def __init__(self):
self.content = dict()
def update(self, item):
if item.unq_id not in self.content:
self.content.update({item.unq_id: item})
return
for k, v in self.content.get(item.unq_id).iteritems():
if k == 'unq_id':
continue
elif k == 'qty':
total_qty = v.qty + item.qty
if total_qty:
v.qty = total_qty
continue
self.remove_item(k)
else:
v[k] = item[k]
def get_total(self):
return sum([v.price * v.qty for _, v in self.content.iteritems()])
def get_num_items(self):
return sum([v.qty for _, v in self.content.iteritems()])
def remove_item(self, key):
self.content.pop(key)
if __name__ == '__main__':
item1 = Item(1, "Banana", 1., 1)
item2 = Item(2, "Eggs", 1., 2)
item3 = Item(3, "Donut", 1., 5)
cart = Cart()
cart.update(item1)
cart.update(item2)
cart.update(item3)
print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
cart.remove_item(1)
print "You have %i items in your cart for a total of $%.02f" % (cart.get_num_items(), cart.get_total())
输出是:
You have 8 items in your cart for a total of $8.00
You have 7 items in your cart for a total of $7.00
答案 2 :(得分:0)
这是一个简单的程序,尝试一下。
#MINI SHOPPING CLI PROGRAM
import time
import random
item = ["Laptop","T-Shirt","Pants"] #You may add your own items
cost = ["Rs 35600","Rs 2000","Rs 1200"] #You may add your own price of your items but make sure you update the program necessarily.
x = []
l = []
print()
for i,j in zip(item,cost):
print(i,"=",j)
print()
choice = 'y' or 'n'
count = 0
while choice == 'y' or choice == 'YES' or choice == 'yes' or choice =='Y':
print()
user_1 = input("Enter the name of the item which you would like to purchase : ")
print()
user_2 = int(input("Enter the cost of the item which you would like to purchase : {} ".format("Rs")))
print()
if user_2 == 35600 or user_2 == 2000 or user_2 == 1200:
l.append(user_2)
else:
print()
print("DONT CHEAT. ABOTING IN 5 SECONDS.")
time.sleep(5)
break
if user_1 in item:
x.append(user_1)
print()
print(user_1,"has been added to your cart.")
print()
count += 1
else:
print()
print("Item not found.Try restarting the app.")
choice = input("Do you want to add any other item to your cart : ")
while choice == 'n' or choice == 'no' or choice == 'NO' or choice == 'N':
print()
print("There are",count,"items in your cart")
print()
print("Total : {}".format("Rs"),sum(l))
print()
user_4 = input("Proceed to checkout (y/n) : ")
if user_4 == 'n':
print()
print("ABORTING IN 5 SECONDS")
time.sleep(5)
break
else:
print()
user_5 = input("Select payment method (Credit/Debit) : ")
print()
print("PROCESSING")
r = 0
while r <= 2:
print(".")
time.sleep(1)
r += 1
print()
print("GENERATING CAPTCHA")
b = 0
while b <= 3:
print(".")
time.sleep(1)
b += 1
print()
print("Enter the captcha given below.")
print()
captcha = random.randint(111111,999999)
print(captcha)
print()
user_6 = input()
if user_6 != captcha:
"ABORTING IN 5 SECONDS."
else:
continue
f = 0
print()
print("AWAITING IMFORMATION.")
while f <= 5:
print(".")
time.sleep(1)
f += 1
print()
print("TRANSACTION SUCCESSFUL.")
print()
print("Thank You for choosing Muhammed©")
break