首页 > 其他 > 详细

Day2:购物车

时间:2017-10-25 22:11:14      阅读:331      评论:0      收藏:0      [点我收藏+]
需求:
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:购物车

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!