I am not talkative, no nonsense, directly on the code:
#!/usr/bin/env python# -*- coding: utf-8 -*-#_auth by kkdef goods_list (): shangpin={"iphone": "6000", "MacAir": "15000", "Tea": "", "book": "The"} print "-" *12 +u "Product List" + "-" *12 for key in shangpin: print "Goods: %s --> price: %s " % (Key,shangpin[key]) print "-"*12 +u" Product List " +"-"*12 return shangpindef jiesuan (money,shangpin_list): sheng_money=money while true: qt= Raw_input (U "Do you need to shop? y/n \n ") if qt==" Y ": gou_goods=raw_input (U "Please enter the name of the product you want to buy, please refer to the product list: \ n") if gou_goods in shangpin_list: sheng_money=int (Sheng_money)-int (Shangpin_list[gou_goods]) if sheng_money>=0: print You have purchased the%s product, total consumption %s, you have %s dollars left! " % (Gou_goods,shangpin_list[gou_goods],sheng_money) else: print "Your money is not enough, please recharge, welcome to the next visit!" " break else: print "No this item! " else: print "Welcome to the next visit!" " breakif __name__== ' __main_ _ ': shangpin_list=goods_list () try: money=raw_input (U "please recharge after the purchase of goods, please enter the amount of recharge: \ n") print "Your recharge amount is %s, the top up is successful!" "% (Money) except IOError,e: print "Input error, please check!" jiesuan (Money,shangpin_list)
This article is from the "Microsoft" blog, so be sure to keep this source http://1238306.blog.51cto.com/1228306/1709677
Python Writing Shopping Cart program