salary = input('welcome to shopping,please input you salary:') #请输入你的工资
product_list = [('iphone--->', 5999), #产品列表
('watch', 599),
('tcl', 799),
('book', 30),
('paper', 15),
('pen', 100)
]
shopoing_list = []
if salary.isdigit(): # 如果输入的工资为数字,执行if语句
salary = int(salary) # 如果这里直接强转int,输入的不是整数就会报错
while True:
for index, i in enumerate(product_list): # 通过enumerate方法取出product_list里的索引
print(index, i)
user_chioce = input('请选择要买的商品序号:')
if user_chioce.isdigit():
user_chioce = int(user_chioce)
if user_chioce < len(product_list) and user_chioce >= 0: #用户输入的数字在可选的范围之内(0-5)
p_item = product_list[user_chioce] #取出用户选择的商品价格
if p_item[1] <= salary:
shopoing_list.append(p_item)
salary -= p_item[1] #将已经用掉的钱从salary中扣除
print('Added %s into shopping cart,your current balance is %s'%(p_item,salary))
else:
print(' balance is insufficient')
else:
print('Product does not exist')
elif user_chioce == 'q':
print('----------SHOPPING LIST----------')
for p in shopoing_list:
print(p)
print('current balance:%s'%salary)
break
else:
print('input error!please enter again!thanks')
else:
print('salary error!')
"""
a = [1,2,3]
for i in enumerate(a): enumerate()方法测试程序
print(i)
"""
""" for i in product_list:
print(product_list.index(i),i) 通过index取出product_list中每个元素的索引即0 1 2 3 4 5
break
"""