本人不善言辞,不废话,直接上代码:
#!/usr/bin/env python# -*- coding: utf-8 -*-#_auth by kkdef goods_list(): shangpin={"iphone":"6000","MacAir":"15000","tea":"50","book":"30"} print "-"*12 +u"商品列表" +"-"*12 for key in shangpin: print "goods: %s --> price: %s" % (key,shangpin[key]) print "-"*12 +u"商品列表" +"-"*12 return shangpindef jiesuan(money,shangpin_list): sheng_money=money while True: qt=raw_input(u"您需要购物吗?y/n \n") if qt=="y": gou_goods=raw_input(u"请输入您想购买的商品名字,购买的商品请参考商品列表:\n") if gou_goods in shangpin_list: sheng_money=int(sheng_money)-int(shangpin_list[gou_goods]) if sheng_money>=0: print " 您购买了%s商品,共消费 %s, 您还剩 %s元钱!" % (gou_goods,shangpin_list[gou_goods],sheng_money) else: print "您的金钱不足,请充值,欢迎下次光临!" break else: print "没有此商品!" else: print "欢迎下次光临!" breakif __name__== '__main__': shangpin_list=goods_list() try: money=raw_input(u"请充值后在购买商品,请输入充值金额:\n") print "您的充值金额为 %s,充值成功!"% (money) except IOError,e: print "输入错误,请检查!" jiesuan(money,shangpin_list)