網站首頁 編程語言 正文
本文實例為大家分享了Python實現用戶名和密碼登錄的具體代碼,供大家參考,具體內容如下
功能
登錄及注冊,密碼錯誤多次后驗證碼確認
說明
初次運行,程序將會自動生成一個名為user的文本文檔,是包含用戶名及密碼的字典
輸入用戶名,如果用戶名不存在,程序會自動以輸入的用戶名進行注冊
輸入密碼,當輸錯4次時,程序會生成一個4位驗證碼,并使用vbs方式彈出,如果驗證碼輸錯,程序退出,否則重新執行主循環
代碼
from os import system from sys import exit from random import randint from time import sleep user={'root':'88888888'} error_time=4 mode=False chack=[None,None] user_name='' user_passwd=[None,None] #讀取用戶 try: ?? ?f=open('user.txt','r') ?? ?user=eval(f.read()) ?? ?f.close() except: ?? ?f=open('user.txt','w') ?? ?f.write("{'root':'88888888'}") ?? ?f.close ?? ?user={'root':'88888888'} #main while True: ?? ?user_name=str(input('請輸入用戶名>')) ?? ?#判斷用戶是否存在 ?? ?if user_name not in user:#用戶不存在 -> 注冊 -> 設置用戶名 ?? ??? ?print('用戶不存在,將執行注冊操作。') ?? ??? ?if ' ' in user_name: ?? ??? ??? ?print('\aErr: 用戶名中不能有空格') ?? ??? ?elif user_name=='': ?? ??? ??? ?print('\aErr: 用戶名不能為空') ?? ??? ?else: ?? ??? ??? ?#設置密碼 ?? ??? ??? ?while True: ?? ??? ??? ??? ?user_passwd[0]=str(input('請設置密碼>')) ?? ??? ??? ??? ?if ' ' in str(user_passwd[0]): ?? ??? ??? ??? ??? ?print('\aErr: 密碼中不能含有空格。') ?? ??? ??? ??? ?elif user_passwd[0]=='': ?? ??? ??? ??? ??? ?print('\aErr: 密碼不能為空。') ?? ??? ??? ??? ?elif len(user_passwd[0])<6: ?? ??? ??? ??? ??? ?print('\aErr: 密碼長度太短,至少6位。') ?? ??? ??? ??? ?else: ?? ??? ??? ??? ??? ?#再次輸入密碼 ?? ??? ??? ??? ??? ?user_passwd[1]=str(input('請再次輸入密碼>')) ?? ??? ??? ??? ??? ?if user_passwd[0]!=user_passwd[1]: ?? ??? ??? ??? ??? ??? ?print('\aErr: 兩次輸入的密碼不一致。') ?? ??? ??? ??? ??? ?else: ?? ??? ??? ??? ??? ??? ?print('注冊成功!\n\n請重新登錄:') ?? ??? ??? ??? ??? ??? ?user[user_name]=user_passwd[0] ?? ??? ??? ??? ??? ??? ?#寫入文件 ?? ??? ??? ??? ??? ??? ?f=open('user.txt','w') ?? ??? ??? ??? ??? ??? ?f.write(str(user)) ?? ??? ??? ??? ??? ??? ?f.close() ?? ??? ??? ??? ??? ??? ?break ?? ?else: ? ?#用戶存在 -> 登錄 -> 確認密碼是否正確 ?? ??? ?#錯4次后驗證碼確認 ?? ??? ?while error_time!=0: ?? ??? ??? ?user_passwd[0]=input('請輸入密碼 4/'+str(error_time)+'>') ?? ??? ??? ?if user_passwd[0]!=user[user_name]: ?? ??? ??? ??? ?print('\aErr: 密碼錯誤') ?? ??? ??? ??? ?error_time=error_time-1 ?? ??? ??? ?else: ?? ??? ??? ??? ?mode=True ?? ??? ??? ??? ?break ?? ??? ?else: ?? ??? ??? ?#驗證碼確認 ?? ??? ??? ?print('\n\a\a因錯誤次數過多,進行驗證碼確認') ?? ??? ??? ?chack[0]=str(randint(999,10000)) ? ?#生成驗證碼 ?? ??? ??? ?#寫入到VBS文件,并彈出 ?? ??? ??? ?f=open('chack.vbs','w') ? ? ?? ??? ??? ?f.write('msgbox("驗證碼>'+str(chack[0])+'<")') ?? ??? ??? ?f.close() ?? ??? ??? ?system('start chack.vbs') ?? ??? ??? ?#驗證驗證碼 ?? ??? ??? ?chack[1]=str(input('請輸入驗證碼>')) ?? ??? ??? ?if chack[0]!=chack[1]: ?? ??? ??? ??? ?print('\aErr: 驗證碼錯誤!') ?? ??? ??? ??? ?#倒計時退出 ?? ??? ??? ??? ?for i in range(3,-1,-1): ?? ??? ??? ??? ??? ?print('\b'*23+'程序將在 '+str(i+1)+' 秒后退出...',end='',flush=True) ?? ??? ??? ??? ??? ?sleep(1) ?? ??? ??? ??? ?exit(0) ?? ??? ??? ?else: ?? ??? ??? ??? ?error_time=4 ?? ?if mode==True: ?? ??? ?break input('登錄成功...')
原文鏈接:https://blog.csdn.net/weixin_45447477/article/details/113376242
相關推薦
- 2023-04-21 C語言哈希表概念超詳細講解_C 語言
- 2022-08-15 使用enum關鍵字定義的枚舉類實現接口的情況
- 2022-04-09 Nginx 提示10013: An attempt was made to access a soc
- 2022-12-04 Dart?異步編程生成器及自定義類型用法詳解_Dart
- 2022-10-20 Swift泛型Generics淺析講解_Swift
- 2022-11-22 GoLang?channel使用介紹_Golang
- 2022-05-12 Kotlin 集合也可以進行+= -= 還可以根據條件進行刪除(removeIf)
- 2022-06-25 分享Pytest?fixture參數傳遞的幾種方式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支