2018-09-09
ide:pycharm2018.02 Python 3.7
Keyword:list Python Basic statement
Explain:
Shopping Cart Procedure
1 run the program, enter the prepayment,
2 Allow user to purchase items based on product number
3user Select the product, check whether the balance is sufficient, enough direct payment, not enough to remind the user
4 May withdraw at any time, when exiting, prints the goods and the balance which has already purchased "
1-------------------------------------------------------------------------------------------------------------- ----
--
Code
--
ImportSYS" "__author__ = "chu ge" Shopping Cart program 1 run the program, enter the prepayment, 2 allows the user to purchase goods according to the product number 3user Select goods, check the balance is sufficient, enough direct payment, not enough to remind User4 can withdraw at any time, exit, Print the goods and balances you have purchased" "product_list=[(' Book', 17.99), ('Phone', 4999), ('cothes', 298), ('Apple', 32.76), (' Break', 98), ('Milk', 55), ('Grape', 25), ('Wine', 198), ('Tea', 76.89), ('biscits', 49) ]#TipsPrint('------------------------------------------------')Print('Input Your Payment') Payment=input (">>>")Print('Payment:%s Dollar'%(Payment))#add empty list to cartShopping_list = []if notPayment.isdigit ():#determine if it is a number, if not exit directly Print('Please enter the amount you are fetching ...') exit ()Else: #if it is a number, continue to executePayment =Int (Payment) whileTrue:#For item in Product_list: #Print (Product_list.index (item), item) # Product_list.index (item) Get subscript #Print product Information forIndex,iteminchEnumerate (product_list):#enumerate the list subscript. Print(Index,item)#Product_list.index (item) get subscriptUser_choice= Input ("Please select the item you want to buy? \n>>>:") #determines whether the input is a number ifuser_choice.isdigit (): User_choice=Int (user_choice)#Judging input length (range) ifuser_choice< Len (product_list) and0 <=User_choice:p_item=Product_list[user_choice]ifP_ITEM[1] <= Payment:#enough to pay to add a shopping cartshopping_list.append (p_item) Payment-= P_item[1] Print("Product information: \033[32;1m%s\033[0m \ t Payment balance: \033[31;1m%s\033[0m yuan"%(p_item,payment))Else: Print("full Insufficient: \033[35;1m%s\033[0m yuan"%(Payment))Else: Print('Product not List ...') elifUser_choice = ='Q':#quit Print("shopping List ....") forQinchshopping_list:Print(q)Print('Your Payment:%s'%payment) exit ()# Break
--
Run Result:
--
------------------------------------------------Input Your Payment >>> 10000payment:10000 $0 (' book ', 17.99) 1 (' Phone ', 4999) 2 (' cothes ', 298) 3 (' Apple ', 32.76) 4 (' Break ', 98) 5 (' Milk ', ' 6 ') 7 (' Wine ', Grape) 8 (' Tea ', 7 6.89) 9 (' Biscits ', 49) Please select the item you want to buy? >>>:1 Product Information: (' phone ', 4999) Payment balance: $5001 0 (' book ', 17.99) 1 (' Phone ', 4999) 2 (' cothes ', 298) 3 (' Apple ', 32 .4 (' Break ', 98) 5 (' Milk ', "6" (' Grape ') 7 (' Wine ', 198) 8 (' Tea ', ' 76.89 ') 9 (' Biscits ', 49) Please select the item you want to buy? >>>:q Shopping List .... (' Phone ', 4999) Your payment:5001 Process finished with exit code 0
--
Python_example_ Commodity settlement procedure