基础要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
扩展需求:
1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
2、允许查询之前的消费记录
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
name_user = {}
验证帐号密码
![]()
1 def con(Username, Pwd):
2 with open('file.user', 'r', encoding='UTF-8') as f:
3 date = eval(f.read().strip())
4 for i in date:
5 if Username == i and Pwd == date[i]:
6 return True
7 else:
8 return False
View Code
写购买记录
![]()
1 def write_shop_file(b):
2 with open('file.user', 'r+', encoding='UTF-8') as f:
3 date = eval(f.read().strip())
4 f.seek(0)
5 f.truncate()
6 date['shopcar']= b
7 f.write(str(date))
8 f.flush()
View Code
写金额
![]()
1 def write_money_file(money):
2 with open('file.user', 'r+', encoding='UTF-8') as f:
3 date = eval(f.read().strip())
4 f.seek(0)
5 f.truncate()
6 date['money'] = money
7 f.write(str(date))
8 f.flush()
View Code
读历史购买记录
![]()
1 def read_shop_file():
2 with open('file.user', 'r', encoding='UTF-8') as f:
3 date = eval(f.read().strip())
4 old = (date['shopcar'])
5 return old
View Code
读历史金额
![]()
1 def read_money_file():
2 with open('file.user', 'r', encoding='UTF-8') as f:
3 date = eval(f.read().strip())
4 money = date['money']
5 return money
View Code
继续购买
![]()
1 def old_buy():
2 money = read_money_file()
3 old_res = read_shop_file()
4 while True:
5 print('----------商品列表------------')
6 for index, i in enumerate(goods):
7 print(index, i['name'], i['price'])
8 num = input('请问需要购买什么?输入商品编号,或者按Q退出:')
9 if num.isdigit():
10 num = int(num)
11 if num >= 0 and num <=len(goods)-1 and money >= goods[num]['price']:
12 old_res.append(goods[num])
13 money -= goods[num]['price']
14 print('你的金额还剩下:''\033[1;31;m%s\033[0m' % money)
15 print('\033[1;31;m%s\033[0m' % '成功加入购物车')
16 elif num >= 0 and num <= len(goods)-1 and money < goods[num]['price']:
17 print('\033[1;31;m%s\033[0m' % '余额不足')
18 else:
19 print('输入商品不存在,请重新输入')
20 elif num == 'q' or num =='Q':
21 if len(old_res) > 0:
22 print('你的金额还剩下:''\033[1;31;m%s\033[0m' % money)
23 print('你总购买商品如下:')
24 for index, i in enumerate(old_res):
25 print('\033[1;31;m', end='')
26 print(index, i['name'], i['price'])
27 print('\033[0m', end='')
28 time.sleep(1)
29 print('欢迎下次光临')
30 write_shop_file(old_res)
31 write_money_file(money)
32 break
33 else:
34 print('输入有误,请重新输入')
35 continue
View Code
新购买
![]()
1 def new_buy():
2 new_res = []
3 money = int(input('请输入金额:'))
4 while True:
5 print('----------商品列表------------')
6 for index, i in enumerate(goods):
7 print(index, i['name'], i['price'])
8 num = input('请问需要购买什么?输入商品编号,或者按Q退出:')
9 if num.isdigit():
10 num = int(num)
11 if num >= 0 and num <=len(goods)-1 and money >= goods[num]['price']: #判断输入是否在索引内
12 new_res.append(goods[num])
13 money -= goods[num]['price'] #没购买一次,金额自减
14 print('你的金额还剩下:''\033[1;31;m%s\033[0m' % money)
15 print('\033[1;31;m%s\033[0m' % '成功加入购物车')
16 elif num >= 0 and num <= len(goods)-1 and money < goods[num]['price']:
17 print('\033[1;31;m%s\033[0m' % '余额不足')
18 else:
19 print('输入商品不存在,请重新输入')
20 elif num == 'q' or num == 'Q':
21 if len(new_res) > 0:
22 print('你的金额还剩下:''\033[1;31;m%s\033[0m' % money)
23 print('你购买商品如下:')
24 for index, i in enumerate(new_res):
25 print('\033[1;31;m', end='')
26 print(index, i['name'], i['price'])
27 print('\033[0m', end='')
28 time.sleep(1)
29 print('欢迎下次光临')
30 write_shop_file(new_res)
31 write_money_file(money)
32 break
33 else:
34 print('输入有误,请重新输入')
35 continue
View Code
登入
![]()
1 def login():
2 # 写帐号密码,判断是否是第一次登入,是就创建
3 with open('file.user', 'r+', encoding='UTF-8') as f:
4 date = f.read()
5 if not date:
6 print('这是您第一次登入,请创建你的帐号和密码')
7 while True:
8 username = input('请输入你要创建的帐号:').strip()
9 pwd = input('请输入你要创建的密码:').strip()
10 pwd2 = input('请再次输入你要创建的密码:').strip()
11 if pwd == pwd2:
12 print('创建成功')
13 time.sleep(0.5)
14 name_user[username] = pwd
15 name_user['money'] = 0
16 name_user['shopcar'] = []
17 print(name_user)
18 f.write(str(name_user))
19 f.flush()
20 break
21 else:
22 print('创建失败,密码输入2次不一致')
23 time.sleep(0.5)
24 continue
25 while True:
26 Username = input('请输入帐号:').strip()
27 Pwd = input('请输入密码:').strip()
28 if con(Username, Pwd):
29 print('登入成功')
30 time.sleep(0.5)
31 money = read_money_file() #用来判断是否是第一次登入,如果是第一次就跳过,直接输入金额
32 if money != 0: # 用-1来做标记
33 print('你的金额还剩下:''\033[1;31;m%s\033[0m' % money)
34 print('你上次的购买记录是:')
35 old_res = read_shop_file()
36 for index, i in enumerate(old_res):
37 print('\033[1;31;m', end='')
38 print(index, i['name'], i['price'])
39 print('\033[0m', end='')
40 flag = input('请问是否继续上次购买,输入Y或者N:').strip()
41 if flag == 'Y' or flag == 'y':
42 old_buy()
43 elif flag == 'N' or flag == 'n':
44 new_buy()
45 else:
46 new_buy()
47 return True
48 else:
49 print('帐号或者密码错误,请重新输入')
50 time.sleep(0.5)
51 continue
View Code