心水Diana

博客园 首页 联系 订阅 管理

#列表嵌套
#输入值效验


list=[('mac',9000),('kindle',8000),('tesla',900000),('python book',105),('bllke',2000)]
salary = input('请输入您的工资:',)
new_list = []
if salary.isdigit(): #效验输入的值是否正确!
salary = int(salary)
# for m in list :
# print(list.index(m),m)
while True :
#打印商品列表
for m ,n in enumerate(list,1): #enumenrete 加入索引打印,1是起始数
print(m,'-'*10,n)
#引导做出商品选择
choice = input('选择购买商品编号[退出:q]')
if choice.isdigit(): #输入值效验是否正确!
choice=int(choice)
#判断输入的商品序号否在列表内
if choice > 0 and choice <=len(list) :
# 将客户选择商品取出来
new_item = list[choice-1]
#判断选择商品价格和余额大小
if new_item[1] < salary :
salary -= new_item[1]
#选择的商品添加到新的列表中
new_list.append(new_item)
print('您已购买商品:',new_list)
else:
print('您的余额不足,还剩%s'%salary)
else:
print('编码不存在')
elif choice == 'q' :
for i in new_list:
print('您已购买的商品:',i)
print('您的余额为%s'%salary)
break
else:
print('输入不合法')
posted on 2018-01-28 19:07  心水Diana  阅读(281)  评论(0编辑  收藏  举报