Example of the shopping cart program. The number of purchased items can be displayed.
Goods_list = [("IphoneX", 9000), ("Tesla", 700000), ("Python book", 55 ),
("Coffee", 35), ("Computer", 6999), ("Bitcoin", 67000), ("Food", 20), ("TV", 5000)]
Shopping_car = []
Cost = 0.0
Print ("Welcome to the shopping market, here is your shopping car .")
Print ("You can enter 'q' to quit at anytime .")
Print ("You can enter 'P' to purchase the goods you buy .")
Print ("You can enter's 'to show your shopping car .")
Print ("You can enter 'R' to show the goods list again .")
Print ("Here are the goods list :")
For I, v in enumerate (goods_list, 1 ):
Print (I, ">>>>>>>>>>>>>>>>>", v)
Saving = input ("Please input your salary :")
If saving. isdigit ():
Saving = float (saving)
While True:
Choose = input ("Please enter the goods NO. you want to buy :")
If choose. isdigit ():
Choose = int (choose)
If choose> 0 and choose <= len (goods_list ):
If goods_list [choose-1] [1] <saving:
Shopping_car.append (goods_list [choose-1])
Cost + = goods_list [choose-1] [1]
Saving-= cost
Print ("Adding % s in your shopping car, it costs % f CNY" %
(Goods_list [choose-1] [0], goods_list [choose-1] [1])
Else:
Print ("Not enough money to buy this goods .")
Else:
Print ("Invalid choosen range .")
Elif choose = "q ":
Print ("Thanks for coming! See you next time! ")
Break
Elif choose = "purchase ":
Print ("Here is your shopping car". center (70 ,"-"))
Shopping_car_set = set (shopping_car)
For goods in shopping_car_set:
Print (goods, "------------- NO:", str (shopping_car.count (goods )),
"---------- COSTS:", goods [1] * shopping_car.count (goods ))
Print ("Total cost: % f CNY" % cost)
Print ("Your saving is now % f CNY" % saving)
Break
Elif choose = "r ":
For I, v in enumerate (goods_list, 1 ):
Print (I, ">>>>>>>>>>>>>>>>>", v)
Elif choose = "s ":
For I in shopping_car:
Print (I)
Else:
Print ("Invalid Input. Try again :-)")