python第三章作业_路飞学城-Python开发集训-第三章-作业

用户登录

作业需求:

基础需求:

让用户输入用户名密码

认证成功后显示欢迎信息

输错三次后退出程序

升级需求:

可以支持多个用户登录 (提示,通过列表存多个账户信息)

用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)

# -*- coding:utf-8 -*-

# __author__ = tom

retry_count = 0

flag = False

while retry_count < 3:

uname = input("请输入用户名:")

lock_user = open('lockaccount','r')

for l in lock_user:

_lname = str(l.strip())

if uname == _lname:

print("用户 %s 被锁定,请联系管理员处理!"%uname)

flag = True

lock_user.close()

if not flag:

user_info = open('account', 'r')

password = input("请输入密码:")

for a in user_info:

_uname = a.strip().split()[0]

_password = a.strip().split()[1]

if uname == _uname and password == _password:

print("欢迎 %s 登录"%uname)

flag = True

break

else:

print("您输入密码不正确!")

retry_count += 1

if flag:

break

else:

openlockfile = open('lockaccount','a')

openlockfile.write('%s''\n'%uname)

openlockfile.close()

print("密码输入错误3次,用户%s已锁定,请联系管理员!"%uname)

account:

tom 123

test 1234

test2 1234

test3 3456

lockaccount:

test1

购物车:

作业需求:

数据结构:

goods = [

{"name":"iphone","price":5888},

{"name":"macbook_pro","price":13888},

{"name":"小米手机","price":2499},

{"name":"鼠标","price":100}

]

功能要求:

基础要求:

1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示

扩展需求:

1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

2、允许查询之前的消费记录

# -*- coding:utf-8 -*-

# __author__ = tom

import json

goods = [

{"name":"iphone","price":5888},

{"name":"macbook_pro","price":13888},

{"name":"小米手机","price":2499},

{"name":"鼠标","price":100}

]

username = "tom"

passwd = "123456"

shopping_car = []

ph_shopping_car =[]

exit_flag = False

count = 0

while count < 3:

user = input("请输入用户名:")

pwd = input("密码:")

if user == username and pwd == passwd:

#判断余额及是否有购买历史

with open("remainder","r") as remainder:

salary_1 = remainder.read()

print("您之前余额为:%s"%salary_1)

print("-----您以往购买过的商品如下------")

with open("purchase_history","r",encoding="utf-8") as ph_1:

tt = ph_1.read()

if len(tt) != 0:

for x in ph_1:

ph_shopping_car.append(x.strip())

for prd1 in ph_shopping_car:

j = json.loads(prd1)

print("%s %s" % (j['name'], j['price']))

else:

print("****您还没有购买过商品****")

#引导进行购买

salary = input("请输入您的工资:")

salary_new = int(salary) + int(salary_1)

while not exit_flag:

print("-------商品列表--------")

for i, prd in enumerate(goods, 1):

print("%s %s %s" % (i, prd['name'], prd['price']))

choice = input("请输入您想购买的商品名称【按q退出】:")

if choice.isdigit():

choice = int(choice)

if choice > 0 and choice <= len(goods):

p_iteam = goods[choice - 1]

price = int(p_iteam['price'])

if price < salary_new:

shopping_car.append(p_iteam)

i = json.dumps(p_iteam, ensure_ascii=False)

with open("purchase_history", "a+", encoding="utf-8") as ph:

ph.write(i)

ph.write("\n")

salary_new -= price

else:

print("余额不足!余额为:\033[1m%s\033[0m"%salary_new)

else:

print("您输入的商品编号不存在!")

elif choice == 'q':

print("您当前余额为:\033[1m%s\033[0m" % salary_new)

with open("remainder","w") as remainder:

remainder.write(str(salary_new))

print('------------您已经购买如下商品----------------')

for i, prd in enumerate(shopping_car):

print("\033[1m%s %s %s\033[0m" % (i, prd['name'], prd['price']))

exit_flag = True

break

else:

print("您输入的用户名或密码不正确,请重新输入!")

count += 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值