1 #!/usr/bin/env python3
2 # -*- coding:utf-8 -*-
3 # Author: davie
4 """
5 功能要求:
6 1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
7 2、允许用户根据商品编号购买商品
8 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
9 4、可随时退出,退出时,打印已购买商品和余额
10 5、在用户使用过程中,
11 关键输出,如余额,商品已加入购物车等消息,需高亮显示
12 """
13 product_list = [
14 {"name":"电脑","price": 1999},
15 {"name":"鼠标","price": 10},
16 {"name":"游艇","price": 20},
17 {"name":"美女","price": 998},
18 {"name":"watch","price":10600},
19 {"name":"iphone","price":8800},
20 {"name":"mac pro","price":14000}
21 ]
22 shopping_list = []
23 username = 'davie'
24 password= 'davie123'
25 user_info = {"username":"davie","password":"davie123","salary":30000}
26 print("----------欢迎登陆----------")
27 _username = input("请输入您的用户名: ").strip()
28 _password = input("请输入您的密码: ").strip()
29 if _username == user_info["username"] and _password == user_info["password"]:
30 salary = input("请输入你的薪资: ").strip()
31 if salary.isdigit():
32 salary = int(salary)
33 while True:
34 print("---------商品列表--开始------------")
35 for idx, item in enumerate(product_list):
36 print(idx, item)
37 print("---------商品列表--结束------------")
38 user_choice = input("请输入选择商品编号,输入q退出: ")
39 if user_choice.isdigit():
40 user_choice = int(user_choice)
41 if user_choice >=0 and user_choice<len(product_list):
42 p_item = product_list[user_choice]
43 if p_item["price"] <= salary:
44 shopping_list.append(p_item)
45 salary -= p_item["price"]
46 print("已经将%s添加到您的购物车了,你的余额是 33[0;31m%s 33[0m元"%(p_item,salary))
47 else:
48 print("你的余额还剩下: 33[31m%s 33[0m元; 无法购买:%s; "%(salary,p_item),'还差: 33[0;31m%s 33[0m元'%(p_item["price"]-salary))
49 else:
50 print("