pythonでカートプログラムを作成する(売り手の入り口を増やし、ユーザー情報を保存する)
36629 ワード
要件:売り手入口: 買手入口:
転載先:https://www.cnblogs.com/BIT-taozhen/p/9760948.html
, , 、
,
, , ,
, ,
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author:James Tao
4
5 import pickle
6 import os
7
8 buyer_or_seller=input(' ! ( ’ ‘\ ’ ‘):')
9
10
11 #-------------------------------- ----------------------------------------------
12 if buyer_or_seller==' ':
13
14 #
15 new_or_existuser=input(' or ( ’ ‘\ ’ ‘):')
16
17
18
19 #-------------------------------- ---------------------------------
20 if new_or_existuser==' ':
21 buyername=input(' ( ):')
22 buyerpasswd=input(' ( ):')
23
24 #
25 initial_salary=int(input(' ! :'))
26
27 #
28 user_passwd={}
29 user_passwd[buyername]=buyerpasswd
30
31 #
32 w_user_passwd = open(buyername+'passwd'+'.txt', 'wb')
33 pickle.dump(user_passwd, w_user_passwd)
34 w_user_passwd.close()
35
36
37 #
38 user_info = {}
39 user_info[buyername]={'balance':initial_salary,'boughtgoods':[]}
40
41 #
42 w_user_info = open(buyername+'info'+'.txt', 'wb')
43 pickle.dump(user_info, w_user_info)
44 w_user_info.close()
45
46 # temp_user_info
47 temp_user_info=user_info
48
49
50
51 #------------------------------ -------------------------------
52 elif new_or_existuser==' ':
53
54 while True:
55 buyername = input(' :')
56 buyerpasswd = input(' :')
57
58 # ,
59 r_user_passwd = open(buyername+'passwd'+'.txt', 'rb')
60 read_user_passwd = pickle.load(r_user_passwd)
61 r_user_passwd.close()
62
63 #
64 if buyername in read_user_passwd and buyerpasswd==read_user_passwd[buyername]:
65
66 # ,
67 r_user_info = open(buyername+'info'+'.txt', 'rb')
68 read_user_info= pickle.load(r_user_info)
69 r_user_info.close()
70
71 # , 、
72 print('''
{_user} , {_balance}
73 '''.format(_user=buyername,_balance=read_user_info[buyername]['balance']))
74 print('------ ------')
75 for i in read_user_info[buyername]['boughtgoods']:
76 print(i)
77
78 # temp_user_info
79 temp_user_info=read_user_info
80 break
81
82 else:
83 print(' , ')
84
85
86 # , 、 、 temp_user_info
87
88 #
89 r_shoppinggoods = open('shoppinggoods.txt', 'rb')
90 read_shoppinggoods = pickle.load(r_shoppinggoods)
91 r_shoppinggoods.close()
92
93 dict_of_bought={}
94
95 # temp_user_info , 、 、 ( )
96 balance=temp_user_info[buyername]['balance']
97 list_of_bought=temp_user_info[buyername]['boughtgoods']#
98
99
100
101
102 #--------------------------------- -------------------------------------
103 while True:
104
105 #
106 print('-------- -------')
107 for index,item in enumerate(read_shoppinggoods):#enumerate
108 print(index,item)
109 #print(read_shoppinggoods.index(item),item)
110 choice_of_user=input(' \ q :')
111
112 #
113 if choice_of_user.isdigit():
114 choice_of_user=int(choice_of_user)
115
116 #
117 if 0<=choice_of_user<len(read_shoppinggoods):
118
119 #
120 if read_shoppinggoods[choice_of_user][1]<= balance:
121
122 #
123 list_of_bought.append(read_shoppinggoods[choice_of_user][0])
124
125 # `
126 balance-=read_shoppinggoods[choice_of_user][1]
127
128 print(''' {_boughtgood} , {_balance}.
129 '''.format(_boughtgood=read_shoppinggoods[choice_of_user][0],_balance=balance))
130 else:
131 print(' , %s, ',balance)
132 else:
133 print(' ')
134
135 elif choice_of_user=='q':
136
137 #
138 category_of_bought=set(list_of_bought)
139 for item in category_of_bought:
140 dict_of_bought[item]=list_of_bought.count(item)
141 print(' ',dict_of_bought)
142
143 print(' :',balance)
144
145 #
146 temp_user_info[buyername] = {'balance': balance, 'boughtgoods': list_of_bought}
147
148 #
149 w_user_userinfo = open(buyername+'info'+'.txt', 'wb')
150 pickle.dump(temp_user_info, w_user_userinfo)
151 w_user_userinfo.close()
152
153 exit()
154 else:
155 print(' ')
156
157
158
159
160
161
162
163
164 #-------------------------------- ----------------------------------------------
165 elif buyer_or_seller==' ':
166
167 # ( , )
168 if os.path.getsize('sellerpasswd.txt') == 0:
169 print(' ')
170
171 sellername = input(' ( ):')
172 sellerpasswd = input(' ( ):')
173
174 #
175 seller_passwd = {}
176 seller_passwd[sellername] = sellerpasswd
177
178 #
179 w_seller_passwd = open('sellerpasswd.txt', 'wb')
180 pickle.dump(seller_passwd, w_seller_passwd)
181 w_seller_passwd.close()
182
183 print(' ! ! 。')
184
185 #
186 while True:
187 print(' ')
188 sellername = input(' :')
189 sellerpasswd = input(' :')
190
191 # ,
192 r_seller_passwd = open('sellerpasswd.txt', 'rb')
193 read_seller_passwd = pickle.load(r_seller_passwd)
194 r_seller_passwd.close()
195
196 #
197 if sellername in read_seller_passwd and sellerpasswd == read_seller_passwd[sellername]:
198 print(' ')
199
200 # , ,
201 if os.path.getsize('shoppinggoods.txt') > 0:
202
203 #
204 r_shoppinggoods = open('shoppinggoods.txt', 'rb')
205 read_shoppinggoods = pickle.load(r_shoppinggoods)
206 r_shoppinggoods.close()
207
208 temp_shoppinggoods=read_shoppinggoods
209
210
211 #
212 print('------ ------')
213 for i in read_shoppinggoods:
214 print(i)
215 else:
216 temp_shoppinggoods = []
217 print(' ')
218
219 #
220 add_or_not = input(' ? A\ ')
221 if add_or_not == 'A':
222 while True:
223 add_goodname = input(' :')
224 add_goodprice = int(input(' :'))
225 add_good = [add_goodname, add_goodprice]
226
227 temp_shoppinggoods.append(add_good)
228
229 continue_or_not = input(' ? C')
230 if continue_or_not == 'C':
231 pass
232 else:
233 break
234
235 #
236 print(' :',temp_shoppinggoods)
237 w_seller_shoppinggoods = open('shoppinggoods.txt', 'wb')
238 pickle.dump(temp_shoppinggoods, w_seller_shoppinggoods)
239 w_seller_shoppinggoods.close()
240
241 break
242
243 else:
244 print(' , ')
転載先:https://www.cnblogs.com/BIT-taozhen/p/9760948.html