1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import getpass 4 import sys 5 f = open('user.db','r') 6 welcome="歡迎來歡樂多購物平臺" 7 out = '謝謝惠顧小店' 8 user_money = 5000000 9 shopping_list = [] 10 shopping_car = {} 11 chose_list = [] 12 goods = [ 13 {"name": "電腦", "price": 1999}, 14 {"name": "鼠標", "price": 10}, 15 {"name": "遊艇", "price": 20}, 16 {"name": "手機", "price": 6000}, 17 {"name": "手錶", "price": 5000}, 18 {"name": "香菸", "price": 100}, 19 {"name": "啤酒", "price": 200}, 20 {"name": "音響", "price": 300}, 21 {"name": "啤酒", "price": 400}, 22 {"name": "紅酒", "price": 1000}, 23 {"name": "牛奶", "price": 600}, 24 {"name": "雪碧", "price": 700}, 25 {"name": "飲水機", "price": 800}, 26 {"name": "機械鍵盤", "price": 3000}, 27 {"name": "錢包", "price": 900}, 28 {"name": "刮鬍刀", "price": 1100}, 29 {"name": "充電寶", "price": 50}, 30 {"name": "咖啡", "price": 1200}, 31 {"name": "眼鏡", "price": 1300}, 32 {"name": "電腦包", "price": 1400}, 33 {"name": "電視機", "price": 4000}, 34 35 ] 36 data = f.read() 37 f.close() 38 user_db_list = [] 39 user_info_list = data.split('\n') 40 print(user_info_list) 41 for item in user_info_list: 42 temp = item.split('|') 43 v = { 44 'name': temp[0], 45 'password': temp[1], 46 'number': temp[2], 47 } 48 user_db_list.append(v) 49 print(welcome) 50 p = 0 51 while True: 52 user_name = input('請輸入您的帳號:') 53 if user_db_list: 54 user_password = input('請輸入您的密碼:') 55 for index,user_list in enumerate(user_db_list): 56 if user_name == user_list['name'] and int(user_list['number']) < 3: 57 if user_password == user_list['password']: 58 print('歡迎',user_name,'登陸咱們平臺','帳號金額:',user_money) 59 one_goods = goods[0:5] 60 for l in one_goods: 61 print(l) 62 print('1頁/6頁') 63 while True: 64 s = input('請輸入頁碼:') 65 s = int(s) 66 start = (s - 1) * 5 67 end = s * 10 68 one_goods = goods[start:end] 69 for i in enumerate(one_goods): 70 shop_id = i[0] 71 shop_name = i[1].get('name') 72 shop_money = i[1].get('price') 73 print('商品id:', shop_id, '商品:', shop_name, '售價:', shop_money) 74 enterone = input('請問你須要選購麼?y/n') 75 if enterone.lower() and enterone == 'y': 76 print('請您輸入你想要的商品id,選購完畢離開請輸入Q/q') 77 while True: 78 choose = input('上帝的選擇:') 79 if choose.lower() and choose == 'q': 80 break 81 elif int(choose) > int(len(one_goods)): 82 print('親愛的上帝,您輸入的數字不在商品列表中,請從新輸入') 83 continue 84 else: 85 now_money = user_money 86 user_money = user_money - goods[int(choose)].get('price') 87 if now_money <= goods[int(choose)].get('price'): 88 print('您的帳號餘額:', now_money, '購買:', goods[int(choose)], '還差:', user_money) 89 # print('加入購物車失敗!') 90 print(""" 91 1 充值 92 2 刪減購物車 93 """) 94 now_chose = input('請選擇:1/2') 95 if int(now_chose) == 1: 96 recharge_money = int(input('請輸入充值金額:')) 97 user_money = user_money + recharge_money 98 print('您的帳號金額:', user_money) 99 elif int(now_chose) == 2: 100 for w in enumerate(chose_list): 101 print(w) 102 while True: 103 remove_list = input('請輸入您想要刪除的商品id:退出請輸入Q/q') 104 now_list = int(len(chose_list)) 105 if remove_list.lower() and remove_list == 'q': 106 print('已成功返回上一層:') 107 break 108 elif int(remove_list) < now_list: 109 g = None 110 e = chose_list[int(remove_list)] 111 for g in enumerate(goods): 112 if str(e) in g[1].get('name'): 113 user_money = now_money + g[1].get('price') 114 print('您如今的餘額爲:', user_money) 115 del chose_list[int(remove_list)] 116 print(chose_list) 117 else: 118 print('輸入有誤請從新輸入') 119 120 else: 121 chose_list.append(goods[int(choose)].get('name')) 122 print(chose_list) 123 print(chose_list) 124 else: 125 print('請稍候....,如今系統爲你自動結算,') 126 print('您的帳號餘額:', user_money) 127 print('您購買的商品:', chose_list) 128 shop_list = open('shopdata', 'w+',encoding='UTF-8') 129 nstr =str(chose_list) 130 shop_list.write(nstr) 131 shop_list.close() 132 # pass 133 sys.exit() 134 else: 135 print('帳號密碼錯誤,請從新輸入') 136 # data_read = open('user.db','r') 137 # # 138 # data_write = "" 139 # data_write = user_list['name'],user_list['number'])= p 140 break 141 else: 142 print('您輸入的是未註冊帳號') 143 break 144 p += 1 145 if p == 3: 146 print('你的嘗試超過限定次數,已退出') 147 break