登录注册(三次机会)

name = input('请注册姓名:')
password = input('请注册密码:')
with open('log',mode='w',encoding='utf-8') as f:
    f.write('{}\n{}'.format(name,password))#格式化输出,两个参数写进一行,write不允许两个变量
print('注册成功')
lis = []
count = 3
while count > 0:
    count -= 1
    with open('log', mode='r+', encoding='utf-8') as f1:
        for i in f1:
            lis.append(i)#读取内容写入到列表中
        name1 = input('请输入登录用户名:')
        if name1 == lis[0].strip():#strip()可以去除\n,\t
            password1 = input('请输入登录密码:')
            if password1 == lis[1]:
                print('\n登录成功!')
                break
            else:
                print('密码错误')
                print('您还有%d次机会'%(count))
                if count == 0:
                    chance = input('机会已用完,是否还想继续?Y(case-sensitive)')
                    if chance.upper() == 'Y':
                        count = 3
                        continue
        else:
            print('用户名错误,请重新输入')
            print('您还有%d次机会'%(count))
            if count == 0:
                chance = input('机会已用完,是否还想继续?Y(case-sensitive)')
                if chance.upper() == 'Y':
                    count = 3
                    continue
else:
    print('\n明天再来吧')

python学习日记(文件操作练习题)

 改变文件数据类型

# 1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数。
# apple 10 3
# tesla 100000 1
# mac 3000 2
# lenovo 30000 3
# chicken 10 3
# 通过代码,将其构建成这种数据类型:[{'name':'apple','price':10,'amount':3},{'name':'tesla','price':1000000,'amount':1}......] 并计算出总价钱。
题目

相关文章:

  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-11-03
猜你喜欢
  • 2021-08-31
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-10-05
  • 2021-11-28
相关资源
相似解决方案