#购物车小程序:思路:创建列表展示清单输入金额存钱创建购物车取列表位置加值输入序号进行购买给相应的值赋购物车判断剩余多少钱 能不能购买该产品输入Q退出展示购物清单与余额!
#__author:"hanhankeji"
#date: 2019/12/4
#购物车小程序:
#建立一个列表,创建商品清单
jiamubiao =[
("mac",9000),
("kindle",800),
("tesla",90000),
("python book",80),
("bike",200),
]
saving = input("please input your saving money >>>:")
shopping_car = []
if saving.isdigit():
saving = int(saving)
# for i in jiamubiao: #for 循环 后面接一个列表
# print(jiamubiao.index(i)+1,i) #取别表的序号0值+1,然后接里面的值,循环出来
while True:
for i,v in enumerate(jiamubiao,1): #展示产品清单
print("序号:",i,v) #展示产品清单
#用户输入商品编码:
choice = input("请选择商品编号购买产品[退出输入:q]:")
if choice.isdigit():
choice = int(choice)
if choice>0 and choice <= len(jiamubiao):
p_item = jiamubiao[choice-1]
if p_item[1] < saving:
saving -= p_item[1]
shopping_car.append(p_item)
else:
print("余额不足!还剩 %s"%saving)
print(p_item)
else:
print("编码不存在!请确认!")
elif choice == "q" :
print("你已购买如下商品")
for i in shopping_car:
print(i)
print("你还剩%s元!"%saving)
break
else:
print("invalid input")
输入123456结果:
please input your saving money >>>:10000
序号: 1 ('mac', 9000)
序号: 2 ('kindle', 800)
序号: 3 ('tesla', 90000)
序号: 4 ('python book', 80)
序号: 5 ('bike', 200)
请选择商品编号购买产品[退出输入:q]:1
('mac', 9000)
序号: 1 ('mac', 9000)
序号: 2 ('kindle', 800)
序号: 3 ('tesla', 90000)
序号: 4 ('python book', 80)
序号: 5 ('bike', 200)
请选择商品编号购买产品[退出输入:q]:2
('kindle', 800)
序号: 1 ('mac', 9000)
序号: 2 ('kindle', 800)
序号: 3 ('tesla', 90000)
序号: 4 ('python book', 80)
序号: 5 ('bike', 200)
请选择商品编号购买产品[退出输入:q]:3
余额不足!还剩 200
('tesla', 90000)
序号: 1 ('mac', 9000)
序号: 2 ('kindle', 800)
序号: 3 ('tesla', 90000)
序号: 4 ('python book', 80)
序号: 5 ('bike', 200)
请选择商品编号购买产品[退出输入:q]:6
编码不存在!请确认!
序号: 1 ('mac', 9000)
序号: 2 ('kindle', 800)
序号: 3 ('tesla', 90000)
序号: 4 ('python book', 80)
序号: 5 ('bike', 200)
请选择商品编号购买产品[退出输入:q]:q
你已购买如下商品
('mac', 9000)
('kindle', 800)
你还剩200元!
进程已结束,退出代码0