鱼C论坛

 找回密码
 立即注册
查看: 1721|回复: 3

[已解决]同一账号输错三次锁定?

[复制链接]
发表于 2020-9-1 16:33:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 看海的猴 于 2020-9-1 16:48 编辑

1:编写用户登录接口
2、输入账号密码完成验证,验证通过后输出"登录成功"
3、可以登录不同的用户.
4、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
  1. inp_uesrname = input("请输入用户名>>>").strip()
  2. inp_password = input("请输入用户密码>>>").strip()
  3. with open(r'user.txt', mode='rt', encoding='utf-8') as f:
  4.     for lien in f:
  5.         username, password = lien.strip().split(":")
  6.         if inp_uesrname == username and inp_password == password:
  7.             print("登陆成功")
  8.             break
  9.         else:
  10.             print("登录失败")
  11.         break
复制代码
最佳答案
2020-9-1 17:33:38
  1. # 1:编写用户登录接口
  2. # 2、输入账号密码完成验证,验证通过后输出"登录成功"
  3. # 3、可以登录不同的用户.
  4. # 4、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
  5. def Read_txt():
  6.     users_psw = {}
  7.     wrong = {}
  8.     with open('users_psw.txt', encoding='utf-8') as file1:
  9.         filecontents = file1.readlines()
  10.         for i in filecontents:
  11.             users = i.replace('\n', '').split(':')[0]
  12.             psw = i.replace('\n', '').split(':')[1]
  13.             users_psw[users] = psw
  14.     print(users_psw)
  15.     with open('wrong.txt', encoding='utf-8') as file2:
  16.         filecontents = file2.readlines()
  17.         for i in filecontents:
  18.             users = i.replace('\n', '').split(':')[0]
  19.             psw = i.replace('\n', '').split(':')[1]
  20.             wrong[users] = psw
  21.     print(wrong)
  22.     return users_psw, wrong

  23. def To_txt(users_psw, wrong):
  24.     with open(f'users_psw.txt', encoding='utf-8', mode='w') as file:
  25.         for k, v in users_psw.items():  # 遍历字典中的键值
  26.             file.write(str(k) + ':')  # 键和值分行放,键在单数行,值在双数行
  27.             file.write(str(v) + '\n')
  28.     with open(f'wrong.txt', encoding='utf-8', mode='w') as file:
  29.         for k, v in wrong.items():  # 遍历字典中的键值
  30.             file.write(str(k) + ':')  # 键和值分行放,键在单数行,值在双数行
  31.             file.write(str(v) + '\n')

  32. def Check_user_psw(user):
  33.     psw = input('请输入密码:')
  34.     if psw == users_psw[user]:
  35.         print('用户名和密码输入正确!')
  36.     else:
  37.         num = 2
  38.         while num > 0:
  39.             psw = input(f'用户名正确,密码错误!请重新输入,还有{num}次机会:')
  40.             if psw == users_psw[user]:
  41.                 print('用户名和密码输入正确!')
  42.                 break
  43.             else:
  44.                 num -= 1
  45.         else:
  46.             print(f'{user}用户密码输入错误,达三次,已锁定!')
  47.             wrong[user] = users_psw[user]
  48.             del users_psw[user]
  49.             To_txt(users_psw, wrong)

  50. while True:
  51.     users_psw, wrong = Read_txt()
  52.     user = input('请输入用户名:')
  53.     if user in wrong:
  54.         print(f'该账户{user}已锁定,不可用!')
  55.         break
  56.     elif user in users_psw.keys():
  57.         Check_user_psw(user)
  58.     else:
  59.         print('不存在该用户!')
  60.         break
  61.     # print(users_psw)
  62.     # print(wrong)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-1 16:37:00 | 显示全部楼层
你不先自己写写么?
有问题再问问题
你这一个需求上来求代码。。。。。。。。。。。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-1 17:33:38 | 显示全部楼层    本楼为最佳答案   
  1. # 1:编写用户登录接口
  2. # 2、输入账号密码完成验证,验证通过后输出"登录成功"
  3. # 3、可以登录不同的用户.
  4. # 4、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
  5. def Read_txt():
  6.     users_psw = {}
  7.     wrong = {}
  8.     with open('users_psw.txt', encoding='utf-8') as file1:
  9.         filecontents = file1.readlines()
  10.         for i in filecontents:
  11.             users = i.replace('\n', '').split(':')[0]
  12.             psw = i.replace('\n', '').split(':')[1]
  13.             users_psw[users] = psw
  14.     print(users_psw)
  15.     with open('wrong.txt', encoding='utf-8') as file2:
  16.         filecontents = file2.readlines()
  17.         for i in filecontents:
  18.             users = i.replace('\n', '').split(':')[0]
  19.             psw = i.replace('\n', '').split(':')[1]
  20.             wrong[users] = psw
  21.     print(wrong)
  22.     return users_psw, wrong

  23. def To_txt(users_psw, wrong):
  24.     with open(f'users_psw.txt', encoding='utf-8', mode='w') as file:
  25.         for k, v in users_psw.items():  # 遍历字典中的键值
  26.             file.write(str(k) + ':')  # 键和值分行放,键在单数行,值在双数行
  27.             file.write(str(v) + '\n')
  28.     with open(f'wrong.txt', encoding='utf-8', mode='w') as file:
  29.         for k, v in wrong.items():  # 遍历字典中的键值
  30.             file.write(str(k) + ':')  # 键和值分行放,键在单数行,值在双数行
  31.             file.write(str(v) + '\n')

  32. def Check_user_psw(user):
  33.     psw = input('请输入密码:')
  34.     if psw == users_psw[user]:
  35.         print('用户名和密码输入正确!')
  36.     else:
  37.         num = 2
  38.         while num > 0:
  39.             psw = input(f'用户名正确,密码错误!请重新输入,还有{num}次机会:')
  40.             if psw == users_psw[user]:
  41.                 print('用户名和密码输入正确!')
  42.                 break
  43.             else:
  44.                 num -= 1
  45.         else:
  46.             print(f'{user}用户密码输入错误,达三次,已锁定!')
  47.             wrong[user] = users_psw[user]
  48.             del users_psw[user]
  49.             To_txt(users_psw, wrong)

  50. while True:
  51.     users_psw, wrong = Read_txt()
  52.     user = input('请输入用户名:')
  53.     if user in wrong:
  54.         print(f'该账户{user}已锁定,不可用!')
  55.         break
  56.     elif user in users_psw.keys():
  57.         Check_user_psw(user)
  58.     else:
  59.         print('不存在该用户!')
  60.         break
  61.     # print(users_psw)
  62.     # print(wrong)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

 楼主| 发表于 2020-9-2 09:53:24 From FishC Mobile | 显示全部楼层
疾风怪盗 发表于 2020-9-1 17:33

函数。。我还没学到谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 17:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表