购物车小程序


 1 #!/usr/bin/python  
 2 # -*- coding:utf-8 -*-  
 3 # Author: kobe
 4 
 5 
 6 
 7 """基础要求:
 8 
 9 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
10 
11 2、允许用户根据商品编号购买商品
12 
13 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
14 
15 4、可随时退出,退出时,打印已购买商品和余额
16 
17 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
18 
19 
20 扩展需求:
21 
22 1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
23 
24 2、允许查询之前的消费记录"""
25 
26 
27 
28 goods = [
29     {"name": "电脑", "price": 1999},
30     {"name": "鼠标", "price": 10},
31     {"name": "游艇", "price": 20},
32     {"name": "美女", "price": 998},
33     {"name": "跑车", "price": 99999}
34 ]
35 goods.append("退出")
36 name = input("what is your name:")
37 password = input("input your password:")
38 salary = int(input("how much your salary:"))
39 
40 for x in goods:
41     if isinstance(x,dict):
42         for k, v in x.items():
43             f =v.split('\n')
44         print(f)
45 
46 
47 
48 
49 for index, items in enumerate(goods):
50     print(index,items)
51 
52 cart = []
53 while True:
54     commodity = int(input("input goods what your want to buy:"))
55     if commodity >= len(goods):
56         print("your input number is over goods's price")
57         continue
58     elif commodity == len(goods) - 1:
59         # salary = salary - goods[commodity]["price"]
60         print("Your balance is \033[1;32m$% s\033[0m!" % salary)
61         print("your have buy some such as \033[1;32m% s\033[0m!" % cart)
62         break
63     else :
64         print(goods[commodity])
65         if goods[commodity]["price"] <= salary :
66             salary = salary - goods[commodity]["price"]
67             cart.append(goods[commodity])
68             print("\033[1;32mThe merchandise has been added to the shopping cart\033[0m!.Your balance is \033[1;32m$% s\033[0m!" % salary)
69         else:
70             print("You have \033[1;31m$% s\033[0m!, \033[1;31mYou don't have enough balance!!!\033[0m!" % salary)

分别列举主流编程语言的特点

优质内容筛选与推荐>>
1、SAS label 和值的互相转换
2、angular-file-upload 限制文件上传个数 获取已上传文件队列
3、systemtap 安装 总结
4、jenkins + Ansible Plugin + ansi-color 让结果显示颜色
5、输入法评价


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号