个人购买程序

'''
需求:
1.启动程序后,让用户输入工资,然后打印商品列表
2.允许用户组根据商品编号购买商品
3.用户选择商品后,检测余额是否够,够即直接扣款,不够就提醒
4.可随时推出,退出时,够就直接退款,不够就提醒
'''

# 定义一个商品列表
shop_list = [
('book', 1000),
('apple air', 5000),
('bike', 7000),
('car', 2000),
]

print(len(shop_list))

# 定义一个商品列表
shop = []

print('------------商品列表如下--------------')
# 使用下标做标记位,用enumerate把shop_list的下角标读到index,shop_list读到item
for index, item in enumerate(shop_list):
print(index, item)

# 现在输入选择商品的id # 输入工资,存在变量salary里面
salary = input('请输入您的工资:')
#判断工资为数字,并赋值为整数型
if salary.isdigit():
salary = int(salary)
#while 死循环
while True:
#输入商品id
user_choice = input('请输入商品id:')
#判断商品id是数字,并赋值为整数型
if user_choice.isdigit():
user_choice = int(user_choice)

if user_choice < len(shop_list) and user_choice >= 0:
#p_item就是存放商品信息的地方
p_item = shop_list[user_choice]
#如果 商品金额小于 工资
if p_item[1] <= salary:
#就把商品信息加到shop列表里面去
shop.append(p_item)
#把工资减了
salary -=p_item[1]
print("你买了{info_1},余额\033[31;1m{info_2}\033[0m".format(info_1=shop, info_2=salary))
else:
print('钱不够了')
break
else:
print('商品不存在')
break

else:
print('按任意字母退出')
break
else:
print('您输入的工资有误')
posted @ 2016-08-02 22:37  wangsen123  阅读(275)  评论(0编辑  收藏  举报