1 #Author:ersa2 " "3 Program: Shopping Cart Program4 5 Requirements:6 7 After you start the program, let the user enter the payroll, and then print the list of items8 allow users to purchase items based on their product number9 after the user selects the product, checks whether the balance is enough, enough on the direct debit, enough to remindTen you can exit at any time to print the purchased goods and balances when exiting One A #知识点: Len (Market): List length (number of entries in the list) - IsDigit () determines whether the input is a number, TRUE is a number - Fetch list Data enumerate the For Index,item in enumerate - Print (index, item) - output highlighting "\033[31;1m%s\033[0m"% (balance) - exit the program using the Exit () method + " " - +Market = [[1],"iphone", 5800], A[2,"Mac Pro", 12000], at[3,"Starbuck Latte", 31], -[4,"Alex Python", 88], -[5,"Bike", 1800]] - -Balance = input ("Please input salary:") - ifbalance.isdigit (): inBalance =int (balance) - Else: toExit"illegal value, please re-enter") + -Amount =0 the Print("Tip:type Q to exit.\n\n") * $Shopping_cart = []Panax Notoginseng whileTrue: - forCommodityinchMarket : the Print(commodity) + AUser_choice = input ("Please enter a product number or q:\n") the + ifUser_choice = ="Q": - Break $ $ ifuser_choice.isdigit (): -User_choice =Int (user_choice) - if0 < User_choice andUser_choice <= (len) +1): theAmount + = Market[user_choice-1][2]; - ifBalance <Amount:Wuyi Print("reminder:the balance is insufficient, please re-purchase.\n") the Continue -Shopping_cart.append (market[user_choice-1]) Wu Else: - Print("If you don't have the this item, please reselect it!\n") About Continue $ - Print("List of Purchased items:\n") - forCommodityinchShopping_cart: - Print(commodity) A Print("Payment Amount: \033[41;1m%s\033[0m"%(amount)) +Balance-=Amount the Print("your balance: \033[31;1m%s\033[0m \ n"%balance)
Python Road Day2 Shopping Cart applet 1