#!/usr/bin/env python
#_*_ coding:utf-8 _*_
#商城仓库(这里用字典模拟)
goods = {‘phone‘:5200,‘camera‘:3500,‘TV‘:2200}
buy = {}
#显示商品清单
def show_goods():
print ‘‘‘
----商品仓--------
ID Name Money
-----------------
商品1、 phone ¥%d
商品2、 camera ¥%d
商品3、 TV ¥%d
go、 去结算
q、 离开
-----------------
‘‘‘ % (goods[‘phone‘],goods[‘camera‘],goods[‘TV‘])
#商城购物
def mall():
while True:
name = raw_input(‘请输入商品名称(或结算、退出):‘)
if name == ‘q‘:
print ‘已退出商城‘
break
if name == ‘go‘:
if len(buy) == 0:
print ‘您没有购买商品,不足以结算‘
continue
else:
break
buy[name] = goods[name]
#结算
def settlement():
print ‘已选商品‘
for k,v in buy.items():
print ‘商品:%s 价格:¥%s‘ % (k,v)
price = sum(buy.values())
print ‘总价:%s‘ % price
while True:
tmp = raw_input(‘是否结算(y/n):‘)
if tmp == ‘y‘:
sums = money - price
if sums < 0:
print ‘对不起金额不足,无法结算‘
break
else:
print ‘结算完成,您的剩余金额为:%s‘ % sums
break
else:
break
if __name__ == ‘__main__‘:
#假设一个金额
money = input(‘输入您的现有金额:‘)
#购物开始
show_goods()
mall()
#进行结算
settlement()
原文地址:http://freshair.blog.51cto.com/8272891/1869605