Optimized the previous shopping cart program: http://www.cnblogs.com/klvchen/p/8577269.html
#输入工资
Salary = input ("Please input your salary:")
#判断工资是否为整数ifSalary.isdigit (): Salary=Int (Salary)Else: Exit ("You must input digit")
#定义购物车cart=[] #商品信息msg= [["iphone6s", 5800], ["mac Book", 9000], ["Coffee", 32], ["Bicycle", 1500]] whileTrue:
#展示商品信息 forKvinchEnumerate (MSG, 1): Print("%s Product Name:%s, Price:%d"% (k, v[0], v[1])) Choice= Input ("Please select the product number, need to purchase! [Q] to exit")
#退出ifChoice = ='Q': Print("========== you have buy ==========") forLinchCart:Print(L) exit ()
#选择商品ifchoice.isdigit (): Choice=int (choice)if0 < Choice <=Len (msg):
#判断余额是否充足ifSalary >= Msg[choice-1][1]: Salary-= Msg[choice-1][1] Print("You had buy%s, money had%d"% (msg[choice-1][0], salary)) Cart.append (Msg[choice-1][0])Else: Print("You don ' t enough money! money have%d"%salary)Else: Print("Please select the right product number!") Else: Print("Please select the right options")
The results of the operation are as follows:
There are a lot of things to optimize the program, and then the time to ponder ~
Python Exercise: A simple shopping cart (ii)