python购物车程序
程序员文章站
2022-03-26 12:19:23
''' 1. 输入用户的工资 2.根据用户的工工资进行选择性的购买 3.最后打印用户购买的产品和用户的余额'''product_list = [ ('iphone', 5000), ('mar pro', 8000), ('ipad', 7000), ('iphone', 15000)]shoppi ......
'''
1. 输入用户的工资
2.根据用户的工工资进行选择性的购买
3.最后打印用户购买的产品和用户的余额
'''
product_list = [
('iphone', 5000),
('mar pro', 8000),
('ipad', 7000),
('iphone', 15000)
]
shopping_list = list()
salary = input('请输入您的工资:')
if salary.isdigit():
salary = int(salary)
while true:
for index, item in enumerate(product_list):
print(index, item)
user_choice = input('请输入您要购买的产品的编号:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary = salary - p_item[1]
print('您的余额是{}'.format(salary))
elif p_item[1] > salary:
print('您的余额不多,无法进行交易')
else:
print('商品不存在')
elif user_choice == 'q':
print('-----shopping list-------')
for p in shopping_list:
print(p)
print('您的余额是{}'.format(salary))
exit()
else:
print('输入有误')