#!/usr/bin/env python
# coding=utf-8
# by 星空刺
qian = int(raw_input("请输入当前money:"))
gongzi = int(raw_input("请输入当前工资:"))
d1 = {}
d2 = {}
d3 = {}
d1 = {'自行车':100,'汽车':1000,'电脑':500}
for k,v in d1.items():
print "%s:%s" %(k,v)
if v <= qian:
d2.update({k:v})
else:
d3.update({k:v})
print "你所不能购买的商品如下:"
for k,v in d3.items():
print "%s:%s" %(k,v)
print "以下是你能购买的商品:"
for k,v in d2.items():
print "%s:%s" %(k,v)
while 1:
name = raw_input("请输入要购买的商品名字:")
name_jiage = d1.get(name)
if d2.has_key(name) and qian >= name_jiage:
qian = qian - name_jiage
print "已经购买%s:%s,当前余额%d" %(name,name_jiage,qian)
else:
chazhi = name_jiage - qian
n = ( chazhi ) / gongzi
if ( name_jiage - qian ) % gongzi > 0:
n = n + 1
print "你的钱不够,还差%d元,你需要赚取%d次工资才能购买" %(chazhi,n)
break
好吧,依然没有用list...虽然它却是是list练习
shell版本:
添加批量购买,购物车,返回操作.
#!/usr/bin/env python
# coding=utf-8
# by 星空刺
# 2014.06.09
import sys
qian = int(raw_input("请输入当前money:"))
gongzi = int(raw_input("请输入当前工资:"))
#商品菜单
d1 = {}
#可购买商品
d2 = {}
#不可购买商品
d3 = {}
#已购买商品
d4 = {}
#商品菜单
d1 = {'自行车':100,'汽车':1000,'电脑':500}
#构建可购买商品字典和不可购买商品字典
for k,v in d1.items():
print "%s:%s" %(k,v)
if v <= qian:
d2.update({k:v})
else:
d3.update({k:v})
print "你所不能购买的商品如下:"
for k,v in d3.items():
print "%s:%s" %(k,v)
print "以下是你能购买的商品:"
for k,v in d2.items():
print "%s:%s" %(k,v)
#商品购买循环
while 1:
name = raw_input("请输入要购买的商品名字:")
name_num = int(raw_input("请输入购买次数:"))
#获取购买商品的价格
name_jiage = d1.get(name)
name_zongjia = name_jiage * name_num
#判定商品是否存在以及钱是否够,并计算余额
if d1.has_key(name) and qian >= name_jiage:
qian = qian - name_zongjia
#若钱不够,则计算差额,并继续所需工资的倍数,若倍数是小数,则+1
else:
chazhi = name_zongjia - qian
n = ( chazhi ) / gongzi
if chazhi % gongzi > 0:
n = n + 1
print "你的钱不够,还差%d元,你需要赚取%d次工资才能购买" %(chazhi,n)
qian = -chazhi
#若购买的商品已在商品篮,则将其对应商品总价相加
if d4.has_key(name):
d4[name] = name_zongjia + d4.get(name)
#若购买商品不存在,则将其加入字典d4
else:
d4.update({name:name_zongjia})
print "已添加%d件%s:%s,当前余额%d" %(name_num,name,name_jiage,qian)
print "当前购物车有如下商品:"
for k,v in d4.items():
print "%s:%s" %(k,v)
ans = raw_input("是否购买其他商品,是y,不是n:")
if ans == "y":
continue
else:
break
print ("以下是你购买车内的商品:")
for k,v in d4.items():
print "%s:%s" %(k,v)
ans = raw_input("是否确认购买y,或者删除某件商品d,或者取消购买n:")
if ans == "y":
print "你的当前余额为%d" %qian
elif ans == "d":
#开始删除循环
while 1:
name_tui = raw_input("输入删除的商品名:")
name_tui_num = int(raw_input("输入删除的商品件数:"))
#计算对应商品的总退价
name_tui_jiage = d1.get(name_tui) * name_tui_num
#如果商品存在,则回款
if d4.has_key(name_tui):
qian = qian + name_tui_jiage
#如果商品存在,且要清空对应商品,则删除字典d4的对应商品
if d4.get(name_tui) == name_tui_jiage:
d4.pop(name_tui)
#否则降低字典d4内对应商品的总价值
else:
d4[name_tui] = d4.get(name_tui) - name_tui_jiage
print "当前余额为%d,商品篮如下:" %qian
for k,v in d4.items():
print "%s:%s" %(k,v)
ans1 = raw_input("是否继续删除y,或者保留剩余商品并购买n:")
if ans1 == "y":
continue
else:
break
elif ans == "n":
for v in d4.values():
qian = qian + v
print "已经清空商品篮,你的当前余额是%d:" %qian