Python 蟒蛇杂货店

Python 蟒蛇杂货店,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,这件事我已经做了几天了 我需要分配项目的货币价值,并将其与相应的项目一起打印出来 这就是我目前得到的 items = [('Diapers', 10), ('Peanut Butter', 5), ('Butter', 6), ('Cheese', 3), ('Milk', 3.5), ('Yogurt', 1.99), ('Eggs', 4.5), ('Bread', 4), ('Shrimp', 2.5), ('Coffee', 1.5)] totalmoney = 50

这件事我已经做了几天了

我需要分配项目的货币价值,并将其与相应的项目一起打印出来

这就是我目前得到的

 items = [('Diapers', 10), ('Peanut Butter', 5), ('Butter', 6), ('Cheese', 
 3), ('Milk', 3.5), ('Yogurt', 1.99), ('Eggs', 4.5), ('Bread', 4), 
 ('Shrimp', 
 2.5), ('Coffee', 1.5)]

 totalmoney = 50

 print ('You have', totalmoney, "dollars!")

 print('This is your options: \n')

 for item in items:
      print(item[0])
然后我需要问用户他们想要什么,它应该从“totalmoney”中拿钱

我想我是通过以下方式做到这一点的:

 if item == item:
      totalmoney - amount

 print(toatalmoney)
如果他们想购买或退货,他们将有能力购买或退货

购买后,我需要打印出他们剩下的总金额

我知道怎么做:

 print(totalmoney)
最后,我必须打印出用户购买了什么以及他们还剩下多少钱。我想我是通过一个变量来实现的,比如:

 itemsincart = ?
 print(itemsincart)
 print(totalmoney)

我建议您阅读更多关于python的教程,因为这是一个相当大的问题。退房,因为你需要他们。另外,请注意问一个好问题。

然后,为了打印购物车中的项目,您必须将它们全部放在一个列表中,并在该列表上迭代。类似于:

for item in cartList:
    print(item[0])

它将打印项目元组的第一个元素,即项目名称。

感谢您的回复!我还想打印出该商品的数量。你可以用字典代替购物车的清单。然后,键可以是商品的名称,值可以是购物车中商品的数量