作业 - 购物车
product_list = [['Iphone7',5800],
['Coffee',30],
['疙瘩汤',10],
['Python Book',99],
['Bike',199],
['ViVo X9',2499],
]
with open('file.txt','w',encoding='utf-8')as f:
f.write('eric|123')
login_flag = False
shopping_cart = []
count = 0
while count < 3:
print('请选择你的操作 \n 1. 登入\n 2. 购物\n 3. 退出')
key = int(input('>>> '))
if key == 1:
username = input('请输入姓名>>>: ')
password = input('请输入密码>>>: ')
with open('file.txt','r',encoding='utf-8')as f:
content = f.readlines()
for i in content:
usn,pwd = i.strip().split('|')
if username == usn and password == pwd:
print('登入成功')
sal = int(input('请输入工资>>>: '))
login_flag = True
else:
print(f'账户名或密码错误{count + 1}次,请重新登入,三次会退出程序')
count += 1
if key == 2:
if not login_flag:
print('请先登入再操作哟~')
continue
print('以下是商品列表')
for id, product in enumerate(product_list,1):
print(f'编号: {id} 商品: {product[0]} 售价: {product[-1]}')
pick_p = input('请选择商品>>>: ')
if sal >= (p_price := product_list[int(pick_p)-1][-1]):
sal -= p_price
shopping_cart.append(product_list[int(pick_p)-1][0])
else:
print(f'您得账户余额不够哟,剩余{sal}')
if key == 3:
print(f'您的购物清单为{shopping_cart},余额为{sal}')
break