Day3购物车优化

 

需求:

  1. 判断用户是否第1次登录,登录过的用户显示上次余额
  2. 商品信息需储存在文档中
  3. 用户购买的商品信息和余额需记录在文档中
  4. 用户在购买过程可随时退出

Readme:

   相关文档:

    1.商品信息文档ShoppingInfo.txt

      iPhone:5000 使用:冒号连接商品和价格

    2.购买信息文档BuyInfo.txt

      注:使用以下格式储存

Your balance:63790
------------shopping list(2018-04-25 11:28:34)-----------
iPhone:5000
MacPro:9800
iPad:3000
Desk:500
Water:10
-----------------------end---------------------------

流程图:

    

 

代码:

  

 1 #-*- coding:utf-8 -*-
 2 #Version:python3.5
 3 
 4 import os,time
 5 
 6 #時間格式
 7 ISOTIMEFORMAT='%Y-%m-%d %X'
 8 time_record = time.strftime( ISOTIMEFORMAT, time.localtime() )
 9 #文件路徑變量
10 shopping_info_file = 'I:\pythontest\ShoppingInfo.txt'
11 buy_info_file = 'I:\pythontest\BuyInfo.txt'
12 
13 shopping_list = []      #從商品信息文件讀取生成列表
14 shopping_cart = []      #已購買商品生成列表
15 
16 #判斷是否第1次登錄,如以前登錄過,則讀取上次剩餘餘額
17 if os.path.exists(buy_info_file):
18     with open(buy_info_file,'r+') as buy_info:
19         for lines1 in buy_info:
20             (user_balance,balance_num) = lines1.strip().split(':')
21             if user_balance == 'Your balance':
22                 balance_num = int(balance_num)
23                 print("%s:%d"%(user_balance,balance_num))
24             break
25 
26 #第1次登錄要輸入工資
27 else:
28     balance_num = input("请输入你的工资:")
29     balance_num = int(balance_num)
30 
31 #打印商品信息
32 with open(shopping_info_file, 'r+') as shopping_info:
33     for lines2 in shopping_info:
34         (product, price) = lines2.strip().split(':')
35         price = int(price)
36         shopping_list.append((product, price))
37 
38 #While循環
39 while True:
40         for index,item in enumerate(shopping_list):
41             print(index,item)
42         choice = input("Your choice is?>>>")
43         if choice.isdigit():
44             choice = int(choice)
45             if choice >=0 and choice < len(shopping_list):
46                 P_list = shopping_list[choice]
47                 if balance_num >= P_list[1]:
48                     shopping_cart.append(P_list)
49                     balance_num -=P_list[1]
50                     print("你选择了:%s,价格:%d,你的余额:\033[31;1m%d\033[0m" % (P_list[0], P_list[1], balance_num))
51                 else:
52                     print("\033[41;1m你选择了:%s,价格:%d,你的余额:%d,不够支付请充值!\033[0m" % (P_list[0], P_list[1], balance_num))
53             else:
54                 print("商品%s不存在" % choice)
55         elif choice == 'q':
56             #針對登錄過的用戶執行以下代碼
57             if os.path.exists(buy_info_file):
58                 buy_info_add = open('I:\pythontest\BuyInfoNew.txt','w')
59                 buy_info_add.close()
60                 with open('I:\pythontest\BuyInfoNew.txt', 'r+') as buy_info_new,open('I:\pythontest\BuyInfo.txt', 'r') as buy_info_old:
61                     buy_info_new.seek(0)
62                     print("你的余额是:%d" % balance_num)
63                     buy_info_new.write("Your balance:%d \n"%balance_num)
64                     print("------------shopping list(%s)-----------\n" % time_record)
65                     buy_info_new.write("------------shopping list(%s)-----------\n"%time_record)
66                     for line in shopping_cart:
67                         print(line)
68                         buy_info_new.write("%s:%s\n"%(line[0],line[1]))
69                     buy_info_new.write("-----------------------end---------------------------\n")
70                     buy_info_old.seek(0)
71                     for line in buy_info_old:
72                         buy_info_new.write(line)
73                 os.remove('I:\pythontest\BuyInfo.txt')
74                 os.rename('I:\pythontest\BuyInfoNew.txt','I:\pythontest\BuyInfo.txt')
75             #新用戶執行以下代碼
76             else:
77                 buy_info_add = open('I:\pythontest\BuyInfo.txt', 'w')
78                 buy_info_add.close()
79                 with open('I:\pythontest\BuyInfo.txt', 'r+') as buy_info_old:
80                     buy_info_old.seek(0)
81                     print("你的余额是:%d" % balance_num)
82                     buy_info_old.write("Your balance:%d \n"%balance_num)
83                     print("------------shopping list(%s)-----------\n" % time_record)
84                     buy_info_old.write("------------shopping list(%s)-----------\n"%time_record)
85                     for line in shopping_cart:
86                         print(line)
87                         buy_info_old.write("%s:%s\n"%(line[0],line[1]))
88                     buy_info_old.write("-----------------------end---------------------------\n")
89             exit()
90         else:
91             #非數字選擇提示錯誤
92             print("Invalid choice,pls rechoice.Tks!")
View Code

 

posted @ 2018-04-24 23:09  Pynetwork  阅读(92)  评论(0编辑  收藏  举报