第二周作业-购物车程序

程序要求

1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录

流程图

 

代码如下:

  1 # -*- coding:utf-8 -*-
  2 # Author:Zhan Chen
  3 '''
  4 程序要求>>>>>
  5 1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
  6 2、允许用户根据商品编号购买商品
  7 3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
  8 4、可随时退出,退出时,打印已购买商品和余额
  9 5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
 10 6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
 11 7、允许查询之前的消费记录
 12 '''
 13 product_list=[
 14     ('iphone',5800),
 15     ('Mac Pro',9800),
 16     ('bike',1000),
 17     ('watch',10000),
 18     ('Coffee',30),
 19     ('ALex Python',99)
 20 ]
 21 fo=open('userinfo.txt','r+',encoding='utf-8')#打开用户信息文件
 22 fo_r=fo.read()#读取文件得到字符串
 23 fo_dict=eval(fo_r)#将字符串数据转化成数组
 24 fo_his=open('histroy.txt','r+',encoding='utf-8')
 25 fo_his_r=fo_his.read()
 26 fo_his_dict=eval(fo_his_r)
 27 # print(fo_dict)
 28 username=input("请输入用户名:")
 29 password=input("请输入密码:")
 30 shoppinglist=fo_his_dict[username]
 31 shoppinglist_now=[]
 32 salary=fo_dict[username][password]
 33 salary=int(salary)
 34 if username in fo_dict:
 35     if password in fo_dict[username]:
 36         print("登陆成功")
 37         # print("工资>>>",str(fo_dict[username][1]))
 38         if fo_dict[username][password]=='':
 39             salary=input("请输入你的工资:")
 40             salary=int(salary)
 41             #将工资写入文件中
 42             fo_dict[username]={password:salary}
 43             fo.seek(0)
 44             fo.write(str(fo_dict))
 45             fo.tell()
 46             #购物程序开始
 47             while True:
 48                 for index, item in enumerate(product_list):
 49                     # print(product_list.index(item),item)
 50                     print(index, item)
 51                 user_choice = input("选择您希望购买的商品>>>>>>:")
 52                 if user_choice.isdigit():
 53                     user_choice = int(user_choice)
 54                     if user_choice < len(product_list) and user_choice >= 0:
 55                         p_item = product_list[user_choice]
 56                         if p_item[1] <= salary:
 57                             shoppinglist_now.append(p_item)
 58                             salary -= p_item[1]
 59                             print("将商品%s添加入购物车,你的余额还剩:\033[31;1m%s\033[0m" % (p_item, salary))
 60                         else:
 61                             print("\033[41;1m你的余额只有[%s]啦,还买个P\033[0m" % (salary))
 62                     else:
 63                         print("商品不存在")
 64                 elif user_choice == "q":
 65                     print("-----购买的商品-----")
 66                     for p in shoppinglist_now:
 67                         print(p)
 68                     print("你的余额:", salary)
 69                     fo_dict[username]={password:salary}
 70                     fo.seek(0)
 71                     fo.write(str(fo_dict))
 72                     fo.tell()
 73                     shoppinglist.extend(shoppinglist_now)
 74                     fo_his_dict[username] = shoppinglist
 75                     fo_his.seek(0)
 76                     fo_his.write(str(fo_his_dict))
 77                     fo_his.tell()
 78                     exit()
 79                 else:
 80                     print("错误选项")
 81         else:
 82             print("您的工资余额为:",fo_dict[username][password])
 83             print("------你目前所购买的物品如下------")
 84             for p2 in shoppinglist:
 85                 print(p2)
 86             print("您的余额为:",fo_dict[username][password])
 87             print("-----请继续购物------")
 88             while True:
 89                 for index, item in enumerate(product_list):
 90                     # print(product_list.index(item),item)
 91                     print(index, item)
 92                 user_choice = input("选择您希望购买的商品>>>>>>:")
 93                 if user_choice.isdigit():
 94                     user_choice = int(user_choice)
 95                     if user_choice < len(product_list) and user_choice >= 0:
 96                         p_item = product_list[user_choice]
 97                         if p_item[1] <= salary:
 98                             shoppinglist_now.append(p_item)
 99                             salary -= p_item[1]
100                             print("将商品%s添加入购物车,你的余额还剩:\033[31;1m%s\033[0m" % (p_item, salary))
101                         else:
102                             print("\033[41;1m你的余额只有[%s]啦,还买个P\033[0m" % (salary))
103                     else:
104                         print("商品不存在")
105                 elif user_choice == "q":
106                     print("-----购买的商品-----")
107                     for p in shoppinglist_now:
108                         print(p)
109                     print("你的余额:", salary)
110                     print("--------结束--------")
111                     fo_dict[username] = {password: salary}
112                     fo.seek(0)
113                     fo.write(str(fo_dict))
114                     fo.tell()
115                     shoppinglist.extend(shoppinglist_now)
116                     fo_his_dict[username] = shoppinglist
117                     fo_his.seek(0)
118                     fo_his.write(str(fo_his_dict))
119                     fo_his.tell()
120                     exit()
121                 else:
122                     print("错误选项")
123     fo.close()
124     fo_his.close()
125 
126 else:
127     print("输入的用户名或密码错误")
View Code

userinfo.txt

{'chenzhan': {'111111': 970}, 'test': {'111111': 111}}}

histroy.txt

{'chenzhan': [('iphone', 5800), ('Coffee', 30), ('Coffee', 30)]}

 

转载于:https://www.cnblogs.com/cz-cangfeng/articles/8118590.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值