這是模擬淘寶的一個簡易的購物商城程序。
用戶入口具有以下功能:
登錄認證
可以鎖定用戶
密碼輸入次數大于3次,鎖定用戶名
連續三次輸錯用戶名退出程序
可以選擇直接購買,也可以選擇加入購物車
用戶使用支付密碼完成支付,支付密碼連續輸入錯誤達3次,鎖定用戶名
商家入口具有以下功能:
登錄認證
可以鎖定用戶
密碼輸入次數大于3次,鎖定用戶名
連續三次輸錯用戶名退出程序
商家可以編輯商品
上架新品
下架商品
修改商品信息:商品名、單價、庫存
每個用戶的用戶名、密碼、余額、支付密碼,以行記錄定義在 user_list.txt 文件中,以逗號分隔;
每件商品的商品名、單價、庫存,以行記錄定義在 product_list.txt 文件中,以逗號加一個空格分隔;
被鎖定用戶名記錄在 lock_list.txt 文件中,以行分隔;
商家的用戶名、密碼定義在 seller_list.txt 文件中,以逗號分隔;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
# Joe Young import getpass import os # 調用os模塊的system方法傳入'cls'參數,清屏 os.system('cls') while True: entrance = input('請選擇:\n\t1. 用戶登陸\n\t2. 商家登陸\n>>>') if entrance != '1' and entrance != '2': print('\n輸入有誤,請重試...\n') else: break # 打印商品列表 def print_product_list(): index = 1 with open('product_list.txt', 'r') as product_file: for product_line in product_file: L = [commodity, price, stock] = product_line.strip('\n').split(', ') commodity_list.append(L) print((str(index) + '. ' + commodity).ljust(20) + ('單價:' + price + '元').ljust(15) + '庫存:' + stock) index += 1 return # 用戶入口 if entrance == '1': info = [] # 存放用戶的信息,初始為空 if_payed = True # if_payed 表示訂單是否已支付 username = '' # 登錄接口 count = 0 while count < 3: username = input ('\n用戶名: ') # 打開鎖定列表文件 with open('lock_list.txt', 'r+') as lock_file: for lock_line in lock_file: # 用戶名在鎖定名單里面,則退出程序 if username == lock_line.strip('\n'): exit('\n用戶名 %s 已被鎖定,請聯系管理員...' % username) login = False # 登錄標志,初始為False # 打開用戶名列表文件,讀權限 user_file = open ('user_list.txt', 'r') for user_line in user_file: # 獲取每行的用戶信息,用戶名、密碼、余額、支付密碼,存入info列表 info = [user, passwd, balance, pay_passwd] = user_line.strip('\n').split(',') # 用戶名匹配,則進入密碼輸入環節 if user == username: n = 0 # 3次輸入機會 while n < 3: password = getpass .getpass('密碼: ') # 密碼匹配,顯示登錄成功 if passwd == password: print('\n歡迎 %s 登錄商城,祝您購物愉快!\n' % username) login = True # 登錄標志賦值為True break # 密碼不匹配 else: # n = 2 時是最后一次機會,不必提示還剩下0次機會 if n != 2: print('\n密碼錯誤,請重新輸入,您還有 %d 次機會\n' % (2-n)) n += 1 # 密碼錯誤次數達到3次,鎖定用戶名,退出程序 else: open('lock_list.txt', 'w').write(username + '\n') exit('\n錯誤次數過多,賬戶已被鎖定...') # 登錄成功,跳出for循環 if login: break else: if count != 2: print('\n用戶名不存在,請重試,您還有 %d 次機會' % (2-count)) user_file.close() count += 1 # 登錄成功,跳出while循環 if login: break else: exit('\n錯誤次數過多,程序已退出...') # 購買程序 shopping_cart = [] # 購物車初始為空 commodity_list = [] print_product_list() while True: i = input ('\n請選擇商品(輸入序號),或輸入 c 取消購買:') if i == 'c': while True: a = input ('\n是否繼續購買?(Y/N):') if a == 'n' or a == 'N': exit('\n交易結束...') elif a == 'y' or a == 'Y': break else: print('\n輸入格式有誤,請重試...') continue if not i.isdigit(): print('\n輸入格式有誤,請重試...') continue i = int (i) if i <= 0 or i > len(commodity_list): print('\n此商品不存在,請重試...') continue item_name = commodity_list[i-1][0] # 商品名稱 item_price = commodity_list[i-1][1] # 商品價格 item_stock = commodity_list[i-1][2] # 商品庫存 print('\n您已選擇了 %s ,請輸入購買的數量,或輸入 b 重新選擇:' % item_name) back = False while True: num = input('>>>') if num == 'b': back = True break if not num.isdigit(): print('輸入格式有誤,請重試...') continue if int(num) > int(item_stock): print('數量大于庫存,請重試...') continue if int(num) == 0: print('數量應大于0,請重試...') break if back: continue item = [item_name, item_price, num] print('\n您已選擇了 %s,單價:%s 元,數量:%s,您想立即購買還是加入購物車?\n' % (item_name, item_price, num)) print('\t1. 立即購買\n\t2. 加入購物車\n') while True: choice = input('>>>') if not (choice == '1' or choice == '2'): print('輸入有誤,請重試...') continue break user_balance = int(info[2]) # 立即購買 if choice == '1': amount = int(item_price) * int(num) count = 0 cancel = False while count < 3: user_pay_passwd = getpass .getpass('\n請輸入支付密碼,或輸入 c 放棄支付:') if user_pay_passwd == 'c': print('\n取消支付成功...') cancel = True break elif user_pay_passwd != info[3]: if count != 2: print('\n密碼錯誤,請重試,您還有 %d 次機會...' % (2-count)) count += 1 else: break if count == 3: with open('lock_list.txt', 'w') as lock_file: lock_file.write(username + '\n') exit('密碼錯誤,賬戶已被鎖定...') if cancel: while True: choice = input ('\n是否繼續購買?(Y/N):') if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'): print('\n輸入格式有誤,請重試...') continue break if choice == 'Y' or choice == 'y': continue else: break # 如果用戶的賬戶余額大于總金額 if user_balance >= amount: user_balance -= amount print('\n支付成功!您已成功購買 %s ,單價:%s 元,數量:%s,總金額:%s 元,賬戶余額:%s 元' % (item_name, item_price, num, amount, user_balance)) lines = open('product_list.txt', 'r').readlines() # 定位到用戶所購買的商品所在行,分割成列表賦值給select select = lines[i-1].strip('\n').split(', ') # 修改商品的庫存 select[-1] = (str(int(select[-1]) - int(num)) + '\n') # 拼接成字符串 lines[i-1] = ', '.join(select) # 將修改寫回文件 open('product_list.txt', 'w').writelines(lines) lines = open('user_list.txt', 'r').readlines() # 修改用戶余額 for line in lines: if username in line.split(','): # 定位到用戶名所在行 j = lines.index(line) # 獲取用戶名所在行的行號索引 select = line.split(',') # 分割用戶名所在行賦值給列表select select[-2] = str(user_balance) # 修改用戶余額 lines[j] = ','.join(select) # 修改后的列表拼接成字符串,覆蓋用戶名所在行 open('user_list.txt', 'w').writelines(lines) # 將修改寫回文件 else: print('\n對不起,您的余額不足...') else: # 加入購物車 j = 0 for j in range(len(shopping_cart)): # 如果商品在購物車里面,更新商品數量 if item_name in shopping_cart[j]: shopping_cart[j][2] = str(int(shopping_cart[j][2]) + int(num)) break # 商品若不在購物車,則添加到購物車 else: shopping_cart.append(item) print('\n成功加入購物車!') while True: choice = input('\n是否繼續購買?(Y/N):') if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'): print('\n輸入格式有誤,請重試...') continue break if choice == 'Y' or choice == 'y': continue else: break # 如果購物車不為空 if shopping_cart: print('\n您的購物車里有以下寶貝:\n') i = 1 total_sum = 0 for item in shopping_cart: (commodity, price, number) = (item[0], item[1], item[2]) print((str(i) + '. ' + commodity).ljust(20) + ('單價:' + price + ' 元').ljust(15) + '數量:' + number) total_sum += int(price) * int(number) i += 1 print('\n合計:%d 元' % total_sum) while True: if_buy = input('\n是否結算?(Y/N):') if not (if_buy == 'Y' or if_buy == 'y' or if_buy == 'N' or if_buy == 'n'): print('\n輸入有誤,請重試...') continue break while True: # 結算 if if_buy == 'Y' or if_buy == 'y': count = 0 cancel = False while count < 3: user_pay_passwd = getpass .getpass('\n請輸入支付密碼,或輸入 c 放棄支付:') if user_pay_passwd == 'c': print('\n取消支付成功...') cancel = True break elif user_pay_passwd != info[3]: if count != 2: print('\n密碼錯誤,請重試,您還有 %d 次機會...' % (2-count)) count += 1 else: break if cancel: if_payed = False elif count == 3: with open('lock_list.txt', 'w') as lock_file: lock_file.write(username + '\n') exit('\n密碼錯誤,賬戶已被鎖定...') else: if total_sum <= user_balance: user_balance - = total_sum print('\n支付成功!您已成功購買以下商品:\n') i = 1 for item in shopping_cart: (commodity, price, number) = (item[0], item[1], item[2]) print((str(i) + '. ' + commodity).ljust(20) + ('單價:' + price + ' 元').ljust(15) + '數量:' + number) lines = open ('product_list.txt', 'r').readlines() for line in lines: # 修改商品庫存 if commodity in line.split(', '): # 定位到商品所在行 j = lines .index(line) # 獲取商品所在行的行號索引 select = line .split(', ') # 商品所在行分割為字符串列表 select[-1] = (str(int(select[-1]) - int(number)) + '\n') # 修改商品庫存 lines[j] = ', '.join(select) # 將修改后的字符串列表組成字符串 open('product_list.txt', 'w').writelines(lines) # 把修改寫回文件 i += 1 lines = open ('user_list.txt', 'r').readlines() for line in lines: # 用戶余額寫入文件 if username in line.split(','): j = lines .index(line) select = line .split(',') select[-2] = str(user_balance) lines[j] = ','.join(select) open('user_list.txt', 'w').writelines(lines) exit('\n合計:%d 元, 賬戶余額:%d 元' % (total_sum, user_balance)) # 不結算 else: print('\n您有一筆未支付訂單...') while True: choice = input ('\n是否進行支付?(Y/N):') if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'): print('\n輸入有誤,請重試...') continue break if choice == 'n' or choice == 'N': exit('\n訂單已取消,感謝光臨購物商城,再見...') else: if_buy = 'Y' continue if not if_payed: print('\n您有一筆未支付訂單...') while True: choice = input ('\n是否進行支付?(Y/N):') if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'): print('\n輸入有誤,請重試...') continue break if choice == 'n' or choice == 'N': exit('\n訂單已取消,感謝光臨購物商城,再見...') else: if_buy = 'Y' continue # 商家入口 if entrance == '2': seller_name = '' # 登錄接口 count = 0 while count < 3: seller_name = input ('\n用戶名:') with open('lock_list.txt', 'r') as lock_file: for lock_line in lock_file: if seller_name == lock_line.strip('\n'): exit('\n用戶名 %s 已被鎖定,請聯系管理員...' % seller_name) seller_file = open ('seller_list.txt', 'r') login = False for seller_line in seller_file: (seller, passwd) = seller_line.strip('\n').split(',') if seller_name == seller: n = 0 while n < 3: password = getpass .getpass('密碼:') # 登錄成功,跳出while循環 if password == passwd: print('\n歡迎 %s 登錄商城' % seller_name) login = True break else: if n != 2: print('\n密碼錯誤,請重試,您還有 %d 次機會' % (2-n)) n += 1 # n = 3 ,鎖定用戶名 else: open('lock_list.txt', 'w').write(seller_name + '\n') exit('\n錯誤次數過多,賬戶已被鎖定...') # 登錄成功,跳出for循環 if login: break # 用戶名不存在 else: if count != 2: print('\n用戶名不存在,請重試,您還有 %d 次機會' % (2-count)) # 登錄成功,跳出while循環 if login: break count += 1 else: exit('\n錯誤次數過多,程序已退出...') # 商品列表編輯程序 L = [] # 存放商品列表,初始為空 commodity_list = [] index = 1 print('\n您的貨架上有以下商品:\n') print_product_list() while True: choice = input ('\n是否編輯您的商品列表?(Y/N):') if not (choice == 'Y' or choice == 'y' or choice == 'N' or choice == 'n'): print('\n輸入有誤,請重試...') continue break if choice == 'Y' or choice == 'y': while True: print('\n請選擇(輸入 q 退出):\n') print('1. 上架新品\n\n2. 下架商品\n\n3. 修改商品信息') choice = input ('\n>>>') if not (choice == '1' or choice == '2' or choice == '3' or choice == 'q'): print('輸入有誤,請重試...') continue # 上架新品 if choice == '1': while True: if_add = False # 是否添加商品的標志,初始為False new_commodity = input('\n輸入商品名:') product_file = open('product_list.txt', 'r') for product_line in product_file: commodity = product_line.strip('\n').split(', ')[0] # 獲取商品列表中的商品名 if new_commodity == commodity: print('\n此商品已在貨架上...') continue else: while True: if_sure = input('\n確定上架新品 %s 嗎?(Y/N):' % new_commodity) if not (if_sure == 'Y' or if_sure == 'y' or if_sure == 'N' or if_sure == 'n'): print('\n輸入有誤,請重試...') continue break # 確定上架新品 if if_sure == 'Y' or if_sure == 'y': while True: # 輸入單價 price = input('\n請輸入單價:') if not price.isdigit(): print('\n輸入有誤,請重試...') continue break while True: # 輸入庫存 stock = input('\n請輸入庫存:') if not stock.isdigit(): print('\n輸入有誤,請重試...') continue break new_line = '\n' + new_commodity + ', ' + price + ', ' + stock open('product_list.txt', 'a').writelines(new_line) print('\n成功上架新品 %s ,單價 %s 元,庫存 %s 件' % (new_commodity, price, stock)) while True: option = input('\n是否繼續添加?(Y/N):') if not (option == 'Y' or option or option == 'N' or option == 'n'): print('\n輸入有誤,請重試...') continue break if option == 'Y' or option == 'y': if_add = True break # 跳出for循環 else: break # 取消上架新品 else: if_add = False break # 跳出for循環 product_file.close() if if_add is True: continue else: break # 跳出while循環 # 下架商品 elif choice == '2': while True: del_num = input('\n請輸入您想下架商品的序號:') if not (del_num.isdigit() or int(del_num) > 0 and int(del_num) <= len(commodity_list)): print('\n輸入有誤,請重試...') continue break del_num = int(del_num) del_commodity = commodity_list[del_num - 1][0] with open('product_list.txt', 'r') as old_file: with open('product_list.txt', 'r+') as new_file: current_line = 0 # 定位到需要刪除的行 while current_line < (del_num - 1): old_file.readline() current_line += 1 # 當前光標在被刪除行的行首,記錄該位置 seek_point = old_file.tell() # 設置光標位置 new_file.seek(seek_point, 0) # 讀需要刪除的行,光標移到下一行行首 old_file.readline() # 被刪除行的下一行讀給 next_line next_line = old_file.readline() # 連續覆蓋剩余行,后面所有行上移一行 while next_line: new_file.write(next_line) next_line = old_file.readline() # 寫完最后一行后截斷文件,因為刪除操作,文件整體少了一行,原文件最后一行需要去掉 new_file.truncate() print('\n您已成功下架 %s !' % del_commodity) # 修改商品信息 elif choice == '3': # 修改商品信息 def mod_commodity_info(i, j): i = int(i) j = int(j) with open('product_list.txt', 'r+') as f: current_line = 0 while current_line < i - 1: f.readline() current_line += 1 seek_point = f .tell() f.seek(seek_point, 0) # 修改商品名 if j == 1: update_line = mod_name () + ', ' + commodity_list[i-1][1] + ', ' + commodity_list[i-1][2] + '\n' # 修改商品價格 elif j == 2: update_line = commodity_list [i-1][0] + ', ' + mod_price() + ', ' + commodity_list[i-1][2] + '\n' # 修改商品庫存 else: update_line = commodity_list [i-1][0] + ', ' + commodity_list[i-1][1] + ', ' + mod_stock() + '\n' f.write(update_line) return def mod_name(): new_name = input ("\n請輸入新的商品名:") return new_name def mod_price(): new_price = input ("\n請輸入新的商品單價:") return new_price def mod_stock(): new_stock = input ("\n請輸入新的商品庫存:") return new_stock # 修改商品單價 def mod_commodity_price(i): i = int (i) with open('product_list.txt', 'r+') as f: current_line = 0 while current_line < i -1: f.readline() current_line += 1 seek_point = f .tell() f.seek(seek_point, 0) new_price = input () while True: i = input ("\n請輸入需要編輯的商品序號(輸入 c 取消):") if not (i.isdigit or i == 'c' or int(i) > 0 and int(i) <= len(commodity_list)): print("\n輸入有誤,請重試...") continue elif i == 'c': break else: while True: j = input("\n請選擇需要編輯的選項(輸入 c 取消):\n\n1. 商品名\n\n2. 單價\n\n3. 庫存\n\n>>>") if not (j == 'c' or j == '1' or j == '2' or j == '3'): print("\n輸入有誤,請重試...") continue break if j == 'c': break else: mod_commodity_info(i, j) else: exit('\n您已退出商城...') else: exit('\n您已退出商城...') |
以上這篇Python 實現購物商城,含有用戶入口和商家入口的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/Dreamer-qiao/archive/2017/09/14/7522952.html