我在这干了几天了。我需要能够在购物车列表中返回用户以前购买的项目。我不知道该怎么办!在
items = [('diapers', 10.00), ('peanuts', 5.00), ('butter', 6.25), ('cheese',
3.00), ('milk', 3.5), ('yogurt', 1.99), ('eggs', 4.5), ('bread', 4),
('shrimp', 2.5), ('coffee', 1.5)]
money = 50
ask = ('buy', 'return', 'quit')
cartList = []
while ask != 'quit':
print("""
Diapers ....10.00
Peanuts ....5.00
Butter .....6.25
Cheese .....3.00
Milk .......3.50
Yogurt .....1.99
Eggs .......4.50
Bread ......4.00
Shrimp .....2.50
Coffee .....1.50
""")
ask = input('Do you want to buy, return or quit?\n').lower()
if ask == 'buy':
item = input('\nWhat item do you need?\n').lower()
for i in items:
if item == i[0]:
print(item, 'is $', i[1])
ques = input('\nDo you want to buy this item?
[Y:N]\n').lower()
if i[1] > money:
print('You do not have enough money for this item :(\n')
break
if ques == 'n':
print('Sorry that you do not want that item :(')
break
if ques == 'y':
money = money - i[1]
print('\nYou now have $', money)
cartList.insert(0, item)
print('You have these items in your cart:\n', cartList,
'\n')
else:
print('You entered an incorrect value :(')
if ask == 'return':
ret = input('What item do you want to return?\n')
for i in cartList:
if ret == i[0]:
print(i[0], '$', i[1])
quest = input('Do you want to return this item?
[Y:N]]\n').lower()
else:
print('This item is not in your cart!\n')
print('\nThank you for shopping with us!')
如果有ask=return,我需要用户能够返回他们购买的项目。我尝试了几次或多次尝试,试图让用户能够返回项目。在
把你认为问题所在的地方插入购物车。你只插入物品,而不是钱。所以在返回部分,必须是:
或者,如果要将项目和金钱同时插入cartList,则应为:
^{pr2}$此外,您还没有为任务(Y/N)构建if-else部分 如果用户输入是Y,则可能需要使用del方法从cartList中删除该元素
除了缺少的逻辑(如从购物车列表中删除返回项)之外,代码中几乎没有小的缩进错误。 试着把所有的东西都打印出来
在您要求customer返回该项之后,请尝试python的remove方法从列表中返回该项。在
^{pr2}$相关问题 更多 >
编程相关推荐