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

学习了list,试写一段购物车程序

时间:2017-07-30 13:51:55      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:for   break   学习   程序   用户   提醒   购物   UI   while   

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # Author:Mgin
 4 shopping_car = []
 5 salary = int(input("your salary:"))
 6 goods_list = [["Watch",20000],["ThinkPad",8500],["IPhone",5800 ],["Suit",3700],["Boost shoes",1700],["Contact Lens",150],["Alex python",80],["Starbucks",30]]
 7 print("goods list:")
 8 for i in goods_list:
 9     print(goods_list.index(i),i)
10 while salary > 0:
11     add = int(input("add goods to your shopping car:"))
12     if add > int(len(goods_list)-1) or add < 0:
13         print("goods have not been found ")
14         stop = input("Do you want to continue:")
15         if stop != n:
16             continue
17         else:
18             for i in shopping_car:
19                 print(i)
20             print("balance:", salary)
21             break
22     elif salary - goods_list[add][1] > 0:
23         salary = salary - goods_list[add][1]
24         shopping_car.append(goods_list[add])
25         print("added",goods_list[add][0],"to your shopping car.","\n","balance:",salary)
26         stop = input("Do you want to continue:")
27         if stop != n:
28             continue
29         else:
30             for i in shopping_car:
31                 print(i)
32             print("balance:",salary)
33             break
34     else:
35         print("you have not enough money")
36         stop = input("Do you want to continue:")
37         if stop != n:
38             continue
39         else:
40             for i in shopping_car:
41                 print(i)
42             print("balance:", salary)
43             break

 程序:购物车

需求:

1.启动程序后,让用户输入工资,然后打印商品列表

2.允许用户根据商品编号购买商品

3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4.可随时退出,退出时,打印已购买商品和余额

学习了list,试写一段购物车程序

标签:for   break   学习   程序   用户   提醒   购物   UI   while   

原文地址:http://www.cnblogs.com/Mgin/p/7258768.html

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