This article is mainly for everyone to introduce the python to achieve shopping cart shopping small program, with a certain reference value, interested in small partners can refer to
Profile
Supposedly, the first small program we get started with is Hello World. Because it is relatively simple, I will not do too much demonstration.
Here is a small program that I wrote. Primarily used to practice Python's basic syntax, as well as getting started.
Main Implementation Functions
Ask the user to enter their expected spending amount.
Display existing product information and ask the user to select
After the user selects the corresponding product label (pay attention to determine whether to exceed the expected amount of consumption, etc.), save to the shopping cart
Display of cart information and remaining amount after user exits
Code:
#!/usr/bin/env python#-*-coding:utf-8-*-"" "@author: Beacon @license:apache licence@file:shopping.py@time:6/16/17 10:05 A M "" "Goods = [(" IPhone ", 5800), (" Watch "," $ "), (" MacBook ", 12000)]goods_cart = []mybudget = input (" Please enter your budget: ") # no consideration is a decimal When not Mybudget.isdigit (): MyBudget = input ("Wrong input, reenter your budget:") MyBudget = Int (mybudget), while True:print ("Commodity list". C Enter ("-")) Print ("number"). Center (8, ""), "name". Ljust (30, ""), "Price". Ljust (Ten, "")) for I in Enumerate (goods): print (Str (i[0]). Center (Ten, ""), str (i[1][0]). Ljust (""), str (i[1][1]). Ljust = input ("Please enter your choice:") if User_choose.isdigit (): user_choose = Int (user_choose) if User_choose >= 0 and User_choose < len (goods): if (mybudget-goods[user_choose][1]) >= 0:goods_cart.append (Goods[user_choose]) mybudget-= Goods[user_ CHOOSE][1] Print ("Budget also%d"% MyBudget) else:print ("Budget Not Enough ~") else:print ("There is no such product ~") Elif user_c Hoose = = ' Q ': Break ELse:print ("Your input is wrong ~") print ("The budget has%d"% (MyBudget)) print ("Shopping cart product Information". Center ("-") ") for I in Enumerate (goods_cart): pri NT (str (I[0)). Center (Ten, ""), str (i[1][0]). Ljust (""), str (i[1][1]). Ljust (10, ""))
Run results