昨晚撸得太嗨,今天简直要死
今天快十一点才就位...
这是这周末的作业...忍不住有改了改
数据结构:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
功能要求:
基础要求:
1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
扩展需求:
1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
2、允许查询之前的消费记录
import os
def save(n, lst, udic, money, y):
userdic['money'] = money
n = n + 's'
with open(n, 'w', encoding='utf-8') as f:
lst[i - 1] = udic
f.write(str(lst))
os.remove(n.rstrip('s'))
os.rename(n, n.rstrip('s'))
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
money = 0
car = []
userdic = {}
lst = []
i = 0
y = 0
f = open('user_message', 'r', encoding='utf-8')
sr = f.read()
if sr != '':
lst = eval(sr)
while 1:
name = input('请输入你的用户名:')
password = input('请输入你的密码:')
for n in lst:
i += 1
if n['name'] == name:
money = int(n['money']) + int(money)
userdic = n
while 1:
if n['password'] == password:
y = 1
break
else:
password = input('密码错误,请重新输入:')
if y == 1:
print('登录成功')
break
g = input('帐号不存在,以此新建帐号输入Y,重新输入输入任意键:')
if g.upper() == 'Y':
userdic['name'] = name
userdic['password'] = password
userdic['money'] = money
userdic['car'] = []
lst.append(userdic)
i = len(lst)
break
while 1:
money = input('请输入你的工资:')
if money.isdigit():
break
print('工资输入错误,请重新输入')
f.close()
while 1:
for n in range(len(goods)):
print(n + 1, goods[n]['name'], goods[n]['price'])
buy = input('请输入你需要的商品号.输入H显示消费记录,输入Q退出:')
if buy == '':
print('输入错误,请重新输入')
continue
elif buy.upper() == 'Q':
print('你已经购买了:', userdic['car'], '\n你的余额为', money)
save('user_message', lst, userdic, money, y)
break
elif buy.upper() == 'H':
print('你购买了以下商品:')
print(userdic['car'])
print('请输入你需要的商品号.输入H显示消费记录,输入Q退出:')
elif int(buy) == 1:
if int(money) >= 1999:
userdic['car'].append(goods[0]['name'].strip())
money = int(money) - 1999
save('user_message', lst, userdic, money, y)
print('你购买了', goods[0]['name'], '余额', money)
else:
print('你的钱不够购买此商品:')
elif int(buy) == 2:
if int(money) >= 10:
userdic['car'].append(goods[1]['name'].strip())
money = int(money) - 10
save('user_message', lst, userdic, money, y)
print('你购买了', goods[1]['name'], '余额', money)
else:
print('你的钱不够购买此商品:')
elif int(buy) == 3:
if int(money) >= 20:
userdic['car'].append(goods[2]['name'].strip())
money = int(money) - 20
save('user_message', lst, userdic, money, y)
print('你购买了', goods[1]['name'], '余额', money)
else:
print('你的钱不够购买此商品:')
elif int(buy) == 4:
if int(money) >= 998:
userdic['car'].append(goods[3]['name'].strip())
money = int(money) - 998
save('user_message', lst, userdic, money, y)
print('你购买了', goods[1]['name'], '余额', money)
else:
print('你的钱不够购买此商品:')
else:
print('输入错误,请重新输入')