網站首頁 編程語言 正文
本文實例為大家分享了Python函數式編程實現登錄注冊功能的具體代碼,供大家參考,具體內容如下
代碼:
def login(username, password): ? ? """ ? ? 用于用戶登錄 ? ? :param username:用戶輸入的用戶名? ? ? :param password: 用戶輸入的密碼 ? ? :return: true表示登錄成功,false登錄失敗 ? ? """ ? ? # 打開文件 ? ? f = open('db', 'r') ? ? # 一行一行遍歷 ? ? for line in f: ? ? ? ? line_list = line.strip().split('|') ?# 去行(\n)拆分 ? ? ? ? if line_list[0] == username and line_list[1] == password: ? ? ? ? ? ? return True ? ? return False def register(username, password): ? ? """ ? ? 用戶注冊功能 ? ? :param username: 用戶名 ? ? :param password: 密碼 ? ? :return: 默認返回null ? ? """ ? ? f = open('db', 'a') ?# 追加模式 ? ? temp = '\n' + username + '|' + password ? ? f.write(temp) ?# 寫入到文件 ? ? f.close() # 主函數 def main(): ? ? t = input('1:登錄 ?2:注冊') ? ? if t == '1': ? ? ? ? user = input('請輸入用戶名:') ? ? ? ? pwd = input('請輸入密碼:') ? ? ? ? r = login(user, pwd) ? ? ? ? if r: ? ? ? ? ? ? print('登錄成功') ? ? ? ? else: ? ? ? ? ? ? print('登錄失敗') ? ? elif t == '2': ? ? ? ? user = input('請輸入用戶名:') ? ? ? ? pwd = input('請輸入密碼:') ? ? ? ? print('注冊成功。') ? ? ? ? register(user, pwd) # 當程序運行時,調用主函數 main()
創建一個db.txt(存儲登錄信息)的文件,內容格式如下:
效果:
登錄
注冊
原文鏈接:https://blog.csdn.net/weixin_44119864/article/details/104232113
相關推薦
- 2022-10-11 Systemtap BPF/BCC bpftrace 實踐對比
- 2022-03-23 .NET微服務架構CI/CD自動打包鏡像_實用技巧
- 2023-03-26 rollup?cli開發全面系統性rollup源碼分析_其它
- 2022-04-27 解決linux下redis數據庫overcommit_memory問題_Redis
- 2022-12-12 Android?SharedPreferences數據存儲詳解_Android
- 2022-12-24 React重新渲染超詳細講解_React
- 2022-03-14 flutter 列表左右滑動帶出按鈕選項
- 2022-03-27 MongoDB的常用命令匯總(Mongo4.2.8)_MongoDB
- 最近更新
-
- 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同步修改后的遠程分支