Program: Shopping Cart Program
Demand:
- After you start the program, let the user enter the payroll, and then print the list of items
- Allow users to purchase items based on their product number
- After the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remind
- You can exit at any time to print the purchased goods and balances when exiting
1Stuff_list = ["1.water, price:500","2.beer, price:600","3.cola, price:700","4.phone, price:800","5.good Water, price:900",#no need to add the backslash, directly in, add on the bottom is good2 "6.movie, price:1000"]3Price_list = [500,600,700,800,900,1000]#This is not recommended, direct price and stuff of the combination of good, you can use a tuple4 5 #product_list = [(' Water, Price: ', $), (' 2.beer, Price: ', +), (' 3.cola, Price: ', '), ("4.phone, Price:", 800) ... This is a good form.6 7Salary = input ("Please input your salary:")8 ifsalary.isdigit ():9Salary =Int (Salary)Ten OneChoice_list = [] A - - whileTrue:#Direct true would be a little better the Print(stuff_list) -Choice = Int (input ("Please input the goods number. If you want to quit,please input-1.")) - ifChoice = =-1: - Print("the goods you have bought be%s, and your money was \033[31;1m%s\033[0m left."%(choice_list,salary)) + Break - Else: + ifPrice_list[choice-1] <= Salary:#Compare prices and remaining amounts A Print("Goods%s is in your shopping car"%choice) atSalary-= Price_list[choice-1] -Choice_list.append (stuff_list[choice-1]) - Else: - Print("Your money isn't enough") - - in " " - to product_list = [#这种表述方式就比我的好, because only one space is used, and there is no problem with the product number because of the order change, the form of the tuple does not appear the problem + (' Iphone ', 5800), - (' Mac Pro ', 9800), the (' Bike ', +), * (' Watch ', 10600), $ (' Coffee '),Panax Notoginseng (' Alex Python ', - ] the shopping_list = [] + salary = input ("Input your Salary:") A if Salary.isdigit (): #判断是否为数字, then convert to int the salary = Int (salary) + While True: - For Index,item in Enumerate (product_list): #高校地找出下标, enumerate this good, subscript, project content $ #print (Product_list.index (item), item) $ print (Index,item) - User_choice = input ("Choose what you want to buy?") >>>: ") - if User_choice.isdigit (): the user_choice = Int (user_choice) - If User_choice < Len (product_list) and User_choice >=0: #还有判断是否用户输入有误, is it inside the productWuyi P_item = Product_list[user_choice] the if p_item[1] <= salary: #买的起 - shopping_list.append (P_item) Wu Salary-= p_item[1] - print ("Added%s into shopping cart,your current balance is \033[31;1m%s\033[0m"% (p_item,salary)) #\033[31;1m%s\033[0m This is the color change, turn 31 red 32 Green 33 yellow function About Else: $ print ("\033[41;1m your balance remains [%s], and buy a sweater \033[0m"% salary) #41是背景的红色, 42 green background - Else: - Print ("Product code [%s] is not exist!" % User_choice) - elif User_choice = = ' Q ': A print ("--------shopping list------") + for P in shopping_list: #好像直接print shopping_list seems to work . the Print (p) - print ("Your Current balance:", salary) $ exit () the Else: the Print ("Invalid option")" "
Python program 2--shopping Cart Applet contact