1.使用以下数据属性和相关方法构建ShoppingCart类。注意:一些方法最初可以是方法存根(空方法),在后面的步骤中完成。
- 注意:我的问题只是我目前陷入的冗长程序的一小部分。程序的其余部分我都做对了,所以它可能看起来不完整。*
1.方法:remove_item()
从cart_items
列表中删除项。有一个字符串(项的名称)参数。不返回任何内容。如果找不到项名称,则输出以下消息:未找到商品。未删除任何内容。
1.实现print_menu()
函数以打印以下选项菜单来操作购物车。
例如:
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
1.实现execute_menu()
函数,它有两个参数:一个代表用户选择的字符和一个购物车。execute_menu()
根据用户的选择执行下面描述的菜单选项。
class ItemToPurchase:
def __init__(self):
self.item_name = 'none'
self.item_description = 'none'
self.item_price = 0
self.item_quantity = 0
def print_item_cost(self):
total = self.item_quantity * self.item_price
print(f'{self.item_name} {self.item_quantity} @ ${self.item_price} = ${total}')
def print_item_description(self):
print(f'{self.item_name}: {self.item_description}')
class ShoppingCart:
def __init__(self, customer_name='none', current_date='January 1, 2016'):
self.customer_name = customer_name
self.current_date = current_date
self.cart_items = []
def remove_item(self, item_name):
for i in self.cart_items:
if i.item_name == item_name:
self.cart_items.remove(i)
else:
print("Item not found in cart. Nothing removed.")
def get_num_items_in_cart(self):
total_items = 0
for i in self.cart_items:
total_items += i.item_quantity
return total_items
def get_cost_of_cart(self):
total_cost = 0
for i in self.cart_items:
total_cost += i.item_price * i.item_quantity
return total_cost
def print_total(self):
print(f'{self.customer_name}\'s Shopping Cart - {self.current_date}')
print(f'Number of Items: {self.get_num_items_in_cart()}\n')
if len(self.cart_items) > 0:
for i in self.cart_items:
i.print_item_cost()
else:
print('SHOPPING CART IS EMPTY')
print(f'\nTotal: ${self.get_cost_of_cart()}')
def print_description(self):
print(f'{self.customer_name}\'s Shopping Cart - {self.current_date}\n')
print('Item Descriptions')
if len(self.cart_items) > 0:
for i in self.cart_items:
i.print_item_description()
else:
print('SHOPPING CART IS EMPTY')
def print_menu():
print('MENU')
print('a - Add item to cart')
print('r - Remove item from cart')
print('c - Change item quantity')
print('i - Output items\' descriptions')
print('o - Output shopping cart')
print('q - Quit')
print()
def execute_menu(choice, cart):
if choice == 'r':
print('REMOVE ITEM FROM CART')
item = input('Enter name of item to remove:\n')
cart.remove_item(item)
print()
if __name__ == "__main__":
menu_choice = ' '
customer_name = input('Enter customer\'s name:\n')
date = input('Enter today\'s date:\n')
print(f'\nCustomer name: {customer_name}')
print(f'Today\'s date: {date}')
print()
my_cart = ShoppingCart(customer_name, date)
print_menu()
while menu_choice != 'q':
menu_choice = input('Choose an option:\n')
if menu_choice in ['a', 'r', 'c', 'i', 'o']:
execute_menu(menu_choice, my_cart)
print_menu()
输入为:
John Doe
February 1, 2016
a
Nike Romaleos
Volt color, Weightlifting shoes
189
2
a
Chocolate Chips
Semi-sweet
3
5
a
Powerbeats 2 Headphones
Bluetooth headphones
128
1
r
Chocolate Chips
o
q
我的输出为:
REMOVE ITEM FROM CART
Enter name of item to remove:
***Item not found in cart. Nothing removed.***
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 3
Nike Romaleos 2 @ $189 = $378
Powerbeats 2 Headphones 1 @ $128 = $128
Total: $506
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
实际输出应为:
REMOVE ITEM FROM CART
Enter name of item to remove:
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 3
Nike Romaleos 2 @ $189 = $378
Powerbeats 2 Headphones 1 @ $128 = $128
Total: $506
MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
2条答案
按热度按时间r3i60tvu1#
我认为问题出在
if
的函数remove_item
中。每个产品的名称与参数中提供的名称不同,都会导致打印"Item not found in cart. Nothing removed."
-因为它会为购物车的每个元素检查循环而不是
我建议:
或者如果有多个对象具有相同的名称:
uelo1irk2#
你的问题似乎是在这个函数.在这里你可以看到for循环检查所有可用的项目.如果它找到项目然后它删除项目.但incase它不这样做,它会打印出“项目未在购物车中找到.没有删除.”为每一个项目它iterates.你可以使用下面的代码代替.