码迷,mamicode.com
首页 > 其他好文 > 详细

Day2:购物车

时间:2017-10-25 23:29:53      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:choice   cart   用户   bag   商品   enumerate   put   you   style   

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


# Author: Ewan Wang
shopping_list = []
goods_list = [
(‘Nike shoes‘,800),
(‘Mjstyle trousers‘,500),
(‘Bag‘,300),
(‘Dictionary‘,100),
(‘Cup‘,80),
(‘Skin lotion‘,60)
]
salary = input(‘Input your salary:‘)
if salary.isdigit():
salary = int(salary)
while True:
for index,item in enumerate(goods_list):
#print(goods_list.index(item),item)
print(index,item)
user_choice = input(‘The product you want to buy is‘)
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(goods_list) and user_choice >=0:
p_item = goods_list[user_choice]
if salary >= p_item[1]:
shopping_list.append(p_item)
salary -= p_item[1]
print(‘Add %s in your shopping cart,your salary balance is %s‘%(p_item[0],salary))
else:
print(‘Your balance is insufficient.‘)
else:
print(‘Invalid option.‘)
elif user_choice == ‘q‘:# 这里是==而非=
print(‘----------shopping list---------‘)
for p in shopping_list:
print(p)
exit()

Day2:购物车

标签:choice   cart   用户   bag   商品   enumerate   put   you   style   

原文地址:http://www.cnblogs.com/akonj3/p/7732636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!