Functional Requirements:
Require users to enter total assets, for example: 2000
Display the list of items, let the user select the item according to the serial number, add the shopping cart
Purchase, if the total amount of goods is greater than the total assets, indicating that the account balance is insufficient, otherwise, the purchase succeeds.
Add: Can recharge, a product to remove the shopping cart
goods = [
{"Name": "Computer", "Price": 1999},
{"Name": "Mouse", "Price": 10},
{"Name": "Yacht", "Price": 20},
{"Name": "Beauty", "Price": 998},
]
shoping=[]goods= [ {"name":"Computer"," Price": 1999}, {"name":"Mouse"," Price": 10}, {"name":"Yacht"," Price": 20}, {"name":"Beauty"," Price": 998},] whiletrue:salary=input ('Your Salary:') ifsalary.isdigit (): Int (Salary) Break Else: Continue whileTrue:#Print (goods) forIndex, IinchEnumerate (goods):Print(Index,'.', i["name"],i[" Price"]) Choice= Input ('Please select a product number to purchase:') Salary=Int (Salary)ifchoice.isdigit (): Choice=int (choice)ifChoice >=0 andChoice <len (Goods): P=Goods[choice]#Print (p) ifSalary >=p[' Price']: Salary-= p[" Price"] Shoping.append (p)Print("Added \033[32;1m[%s]\033[0m into your shopping cart,and you are balance"%(p['name'], salary))Else: Print("you don't have enough money.") Chongzhi=input ('If you need to recharge y/n:') ifChongzhi = ="Y" orChongzhi = ='y': Salary_chongzhi=input ("Please enter the amount of the recharge") ifsalary_chongzhi.isdigit (): Salary_chongzhi=Int (Salary_chongzhi) Salary=salary_chongzhi+SalaryElse: Continue elifChoice = ='Delete': forIndex, IinchEnumerate (shoping):Print(Index,'.', i['name'], i[' Price']) Delete= Input ('Please enter the item number you want to delete:') ifdelete.isdigit (): Delete=Int (delete)#if delete >=0 and delete < Len (shoping):salary+=shoping[delete][' Price'] Shoping.pop (delete)elifChoice = =' Shop': Print(shoping)elifchoice=="quit": Print("List of items you purchased:", Shoping) exit ()View Code
Python Shopping Cart