Description: 1. After starting the program, let the user enter the amount and then print out all the items and their prices if the input is not a number then exit the program 2. Allow the user to enter the product number to buy the product if the number is entered is reasonable, if reasonable then deduct money, add the user's shopping cart if the input is Q exit process If you enter a different character or symbol, you are prompted to enter the error, ask if you re-enter the Y will continue to let the user enter the product serial number or exit the program, and print out the goods have been purchased 3. Print the purchased product flowchart when exiting the program
650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/9E/9D/wKiom1mTFzfj5CGLAABP1Elgcnw022.png-wh_500x0-wm_ 3-wmp_4-s_1045710521.png "title=" Shopping cart flowchart. png "alt=" wkiom1mtfzfj5cglaabp1elgcnw022.png-wh_50 "/>
#! usr/bin/evn python#-*-coding utf-8 -*-product_list = [ ( "BMW", ", " ("Volkswagen", ", ") ("Toyota", ", ") ("Bentley") , 1100), ("Honda", 950),] #创建一个produce_list字典存放产品及价格shooping_cart =dict () # Create a dictionary to store the products that the user has purchased, and the number of items purchased While true: remain_sum = input ("Please enter your balance:") if remain_sum.isdigit (): print (" Your shopping cart is empty ..... ") remain_sum=int (remain_sum) loop = 0 while True: if loop ==0: #只有用户刚进入的时候显示余额, That is, the balance is printed the first time the while loop is executed print ("|-----------|\n| YuAmount ", Remain_sum," \n|___________| ") loop =1 print ("= = = Product List = = = = = = =) for index,product in enumerate (product_list): print (Index,product[0], PRODUCT[1]) while True: user_choice = input ("Please enter the serial number of the item you want to buy") if user_choice.isdigit (): #判断输入的是否是数字 user_choice = int (user_ Choice if -1< user_choice<len (product_list): #判断输入的商品编号是否存在 product = product_list[user_choice][0] pice = product_list[user_choice][1] if remain_sum >= pice:# Only items that are not more than the balance will be deducted and added to the shopping cart remain_sum -=pice if product in shooping_cart: #将购买的商品加入用户购物车, and count the number of purchases shooping_cart[product] +=1 else: shooping_cart[product]=1 break else: print ("----> Your balance is%s, Please select another product <-----"%remain_sum) recharge = input ("Would you like to recharge:") if recharge == "Y": add = input ("Please enter the amount you recharge:") while not add.isdigit ( ): Add = input ("Your input is wrong, please re-enter the amount you recharge:") remain_sum += int (ADD) print ("The balance After you recharge is:", remain_sum) break else: break else: print ("----> The item you selected does not exist <-----") elif user_choice == "Q": #如果用户输入的是q而不是商品编号则推出程序, and print out the items purchased by the user &nbsP; if len ( Shooping_cart) ==0: print ("Gently you go, as you gently come, without a product") else: print ("You have purchased the following items:") Key = list (Shooping_cart.keys ()) for item in key: print (Item, ":", Shooping_cart[item]) exit () else:# If the user enters neither the product number nor q then prompts for an incorrect input and decides whether to continue shopping if input ("Your input is wrong" "\n<----> whether to continue shopping ^_^ : ") = =" Y ": continue else: #如果用户不输入y, print the product and quantity purchased by the user, and launch the program &nBsp; if len (shooping_cart) = = 0: print ("Gently you go, as you gently come, without a product") else: print ("You have purchased the following items:") key = list (Shooping_cart.keys ()) for item in key: print (Item, ":", Shooping_cart[item]) exit () else: Print ("Your currency is not circulating, please return to the renminbi for shopping!!!" if input ("Please continue shopping" ") = =" Y ": continue else: print ("You have left the store and welcome the next visit ...") exit ()
This article is from the "Pure mash Eight cup vsit" blog, please be sure to keep this source http://chunlaobazhong.blog.51cto.com/11499723/1956608
"We learn python together"-Program Exercise: Shopping Cart Program