Classmate code, Shopping cart.
#!/usr/bin/env python__author__='Liudong'#define an empty shopping list:Cart = []#Query User BalancesImportOs,sysdefMoney (): filename= sys.path[0]+'/money.txt' ifos.path.exists (filename): M= Open ('./money.txt','R') Mon=m.readline () m.close () Mon=Int (Mon)returnMonElse: Exitt=False while notExitt:aa= Input ("\033[31m Please enter your salary: \033[0m"). Strip ()ifaa.isdigit (): Mon=int (aa) Exitt=TrueElse: Print("Please enter the correct number: ") returnMon#Save the balance entered by the userdefFree_money (x): a= Open ('./money.txt','W') A.write ('%s'%x) a.close ()#Define balance VariablesWage =Money () Wage1= Wage#Define total BalancesShoping_money3 = 0#Define total user consumption#Define the Product list:Shop = [ ["Condoms", 100], ["pill", 99], ["girlfriend", 1], ["mac Book", 8888], ["Apple", 5], ["le vision 1S", 1099]]#Cycle through shopping information: whileTrue:#print out a list of items and prices: Print('\33[32m Welcome to \33[37m'. Center (50,'-')) Print('\33[32m Product List \33[37m'. Center (50,'-')) forIndex,pinchEnumerate (Shop):Print(index,p[0],p[1]) #get user input:Shoping = input ("Please enter the product number, settlement Please enter \33[32mall\33[37m, display cart Please enter: \33[32mlist\33[37m, show balance Please enter: \33[32mfree\33[37m:"). Strip ()#Determine if user input is legal ifshoping.isdigit (): Shoping=Int (shoping)ifLen (Shop) <shoping:Print('\33[31m The product number you entered does not exist, please re-select the product number! \33[37m') ContinueShoping_money= Shop[shoping][1] ifShoping_money < wage:#determine if a user can buy whileTrue:num= Input ("Please enter purchase quantity:"). Strip ()if notnum.isdigit ():Print("\033[31m Please enter the quantity of goods correctly. \033[0m") Else: Num=int (num)ifnum = =0: Break Else: Shoping_money2= Shoping_money * num#determine the price of a product quantity ifShoping_money2 < wage:#determine if a user can buyCart.append (Shop[shoping][0])#Add the product name to the shopping cartCart.append (num)#Add Item quantity to Cart Print("\033[31m Your item has been added to cart%s number:%s \033[0m"%(shop[shoping][0],num)) wage= Wage-shoping_money2#figure out the remaining amount for the userShoping_money3 +=shoping_money2#figure out the total amount of user consumption Break Else: Print("\033[31m Your wages can't afford to buy%s%s \033[0m"%(Num,shop[shoping][0])) Break elifShoping_money > wage:#tips for items that cannot be purchased Print("\033[31m you can't afford to pay for this item:%s\033[0m"%shop[shoping][0])Else:#tips for items that cannot be purchased Print("\033[31m you pay too little, or try to make money. \033[0m") Break elifShoping = ="List":#Show Shopping list Print("\033[31m----------Shopping list----------\033[0m") Num=0 AA=len (cart) BB= Int (AA/2) forXinchRange (BB):Print(Cart[num],"number of \033[31m: \033[0m", cart[num+1]) Num+=2Print("\033[31m---------------------------------\033[0m") elifShoping = =" Free":#Show Remaining Amount if notlen (cart):Print("\033[31m You haven't shopped yet, the rest:%s\033[0m"%wage)Else: Print("\033[31m one spends altogether:%s, remaining:%s\033[0m"%(shoping_money3,wage))elifShoping = =" All":#User Settlement if notLen (cart):#determine if the shopping cart is empty Print("\033[31m Your Shopping cart is empty and does not need to be settled. \033[0m") Free_money (wage) Break Else:#shopping cart is not empty, show shopping list and purchase item quantity Print("\033[31m----------Shopping list----------\033[0m") Num=0 AA=len (cart) BB= Int (AA/2) forXinchRange (BB):Print(Cart[num],"number of \033[31m: \033[0m", cart[num+1]) Num+=2Print("\033[31m---------------------------------\033[0m") GM= Input ("\033[31m Confirm purchase: Y or N \033[0m"). Strip ()#determine whether a user buys a product ifGM = ="N" orGM = ="N":#users do not purchase Print("you did not buy any goods, the remaining: \033[31m%s\033[0m"%wage1) Free_money (wage1) Break elifGM = ="y" orGM = ="Y":#User Purchase Print("you spent \033[31m%s\033[0m and \033[31m%s\033[0m ."%(Shoping_money3,wage)) Free_money (wage) Break Else:#error input, default user does not purchase Print("you entered the incorrect, did not buy any goods, the remaining: \033[31m%s\033[0m"%wage1) Free_money (wage1) Break
View Code
Students out of the code, shopping cart.