python 作业 购物车

作业要求:

1、启动程序后,输入用户名,密码,如果第一次登录,让用户输入工资,然后打印商品列表

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

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

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

5、在用户使用过程中,关键输出,如余额,商品已加入购物车等消息,需高亮显示

6、用户下一次登陆后,输入用户名,密码,直接回到上次的状态,即上次消费的余额什么的,还是那些,再次登录可继续购买

7、允许查询之前消费记录

 

流程图:

 

user_info_list.txt:

{'jfsu': {'password': 'abc123!', 'salary': 30000, 'buying_list': []}, 'mmm': {'password': '123123', 'salary': 50000, 'buying_list': []}}

 

  1 __author__ = 'sujunfeng'
  2 
  3 shopping_menu=[
  4     ("Iphone X",8999),
  5     ("Mac pro",17999),
  6     ("Bike",999),
  7     ("Balance car",1999),
  8     ("Book",59),
  9     ("IWatch",3999)
 10 ]
 11 
 12 import sys
 13 with open("user_info_list.txt",'r') as f:
 14     info=f.read()
 15     user_info_dict = eval(info)
 16 
 17 def shopping_list():  # 打印商品列表
 18     print('\033[31;1m商品列表\033[0m'.center(50,'-'))
 19     for menu in shopping_menu:
 20         #print(menu[1][1])
 21         print(shopping_menu.index(menu),menu)
 22 
 23 
 24 user_login = input("\033[31;1m请输入您的用户名:\033[0m")  # 输入用户名
 25 
 26 if user_login in user_info_dict:
 27     balance = user_info_dict[user_login]['salary']  # 定义用户余额
 28     print('\033[42;1m 您的余额为:%s\033[0m' % balance)  # 显示余额
 29 
 30     this_buy = user_info_dict[user_login]['buying_list'] #初始化本次购买记录
 31     print(this_buy)
 32     shopping_list()  # 打印商品列表
 33 
 34     exit_flag = False
 35     while not exit_flag:
 36         choice = int(input("\033[31;1m请输入您要购买的商品编号:\033[0m"))  # 选择购买商品编号
 37         if choice < len(shopping_menu) and choice >= 0:   # 判断输入编号是否在购买编号范围内
 38             shopping_item = shopping_menu[choice]         # 定义购买商品信息
 39             #print(shopping_item)
 40             if balance >= shopping_item[1]:  # 判断金额是否足够
 41                 balance = balance - shopping_item[1]    # 金额足够则在原有基础上减去商品金额
 42                 product_name = shopping_item[0]  # 定义购买商品信息
 43 
 44                 this_buy.append(product_name)  # 定义本次购买商品信息
 45                 print("\033[42;1m您已购买的商品是:%s\033[0m" % this_buy)
 46                 print("\033[42;1m您的余额为:%s\033[0m" % balance)
 47 
 48                 go_on = input("\033[41;1m是否继续购买 按'y'继续购买,或者按任意键退出:\033[0m")
 49                 if go_on == 'y':
 50                     shopping_list()
 51                     continue
 52                 else:
 53                     print("\033[42;1m您的余额为 %s \033[0m" % balance)
 54                     print("\033[42;1m您购买的商品是:%s \033[0m" % this_buy)
 55 
 56                     #购买状态写入用户文件
 57                     with open('user_info_list.txt','w') as f:
 58                         user_info_dict[user_login]['buying_list'] = this_buy  # 用户已购买列表
 59                         user_info_dict[user_login]['salary'] = balance   # 用户余额
 60                         #print(str(user_info_dict[user_login]['buying_list']))
 61                         #print(user_info_dict)
 62                         f.write(str(user_info_dict))
 63                     sys.exit()
 64             else:
 65                 print("\033[41;1m您的余额不足,请充值,您的余额为 %s \033[0m" % balance)
 66                 is_recharge = input("\033[31;1m是否充入金额 y 选择充入金额,或者按任意键退出\033[0m")
 67                 if is_recharge == 'y':
 68                     user_salary = input("\033[31;1m请充入您的金额:\033[0m")   # 提示输入金额
 69                     if user_salary.isdigit():  # 金额为数字
 70                         user_salary=int(user_salary)  # 金额变为整形
 71                         current_balance = user_salary +  balance # 充值后总金额 =  充值金额 + 余额
 72                         user_info_dict[user_login]['salary'] = current_balance
 73 
 74                         with open('user_info_list.txt','w',encoding="utf-8") as f_init:
 75                             f_init.write(str(user_info_dict))  # 所有用户字典写回用户信息文件
 76                         continue
 77                     else:
 78                         print("\033[31;1m您的输入不正确,请输入充值金额的数字!\033[31;1m")
 79                         break
 80                 else:
 81                     print("\033[41;1m您的余额为 %s \033[0m" % balance)
 82                     sys.exit()
 83 
 84 
 85 
 86 else:
 87     print("\033[31;1m您的用户不存在,需要注册!\033[0m")
 88     new_user_password = input("\033[31;1m请输入您的密码:\033[0m")  # 输入新用户密码
 89     exit_flag = False
 90     while not exit_flag:
 91         if new_user_password.strip() != "":  # 新密码不为空
 92             new_user_salary = input("\033[31;1m请充入您的金额:\033[0m")   # 提示输入金额
 93             if new_user_salary.isdigit():  # 金额为数字
 94                 new_user_salary=int(new_user_salary)  # 金额变为整形
 95                 new_user_info = {user_login:{'password':new_user_password,'salary':new_user_salary}} # 定义新用户字典信息
 96 
 97                 user_new_dict = dict(user_info_dict,**new_user_info)    # 合并新用户字典到 所有用户字典
 98 
 99                 with open('user_info_list.txt','w',encoding="utf-8") as f_init:
100                     user_all_dict = f_init.write(str(user_new_dict))  # 所有用户字典写回用户信息文件
101 
102                 print('\033[31;1m登录成功,欢迎您 %s 您的余额为:%s\033[0m'% (user_login,new_user_salary))
103                 shopping_list()  # 打印商品列表
104                 break  #退出(需要添加跳转到用户购买流程,暂时不会定义函数)
105             else:
106                 print("\033[31;1m您的输入不正确,请输入充值金额的数字!\033[0m")
107                 exit_flag = False # 跳转到金额输入
108         else:
109             print('\033[42;1m您的密码输入不符合要求,程序退出\033[0m')
110             sys.exit()
View Code

 

posted @ 2017-12-02 15:59  boundshunter  阅读(165)  评论(0编辑  收藏  举报