Python實現簡單用戶注冊信息管理系統


運行效果:

 

 

注意:運行前請在同一目錄下創建一個userdata.bin用於保存用戶數據

 

源代碼:

 1 # coding:utf-8
 2 '''
 3 用戶注冊信息管理系統  4 功能包括:  5  1.查看全部已注冊用戶信息  6  2.查找用戶信息  7  3.修改用戶信息  8  4.刪除用戶信息  9  5.添加新用戶  10  6.將用戶信息存入文件  11 每個注冊用戶的信息用對象表示,程序啟動時,自動載入文件中保存的用戶信息  12 程序啟動后,顯示操作菜單,並根據選擇執行不同的操作  13 各種菜單操作定義為函數,調用函數完成對應操作  14 '''
 15 '''
 16 導入pickle模塊中的dump、load方法  17 dump方法將對象寫入文件,load方法從文件中載入對象  18 '''
 19 
 20 from pickle import dump,load  21 
 22 ##定義user類,實例對象的userName屬性存儲用戶名,passWord屬性存儲登錄密碼
 23 
 24 class user:  25     #實例化對象,默認是None
 26     def __init__(self,userName=None,passWord=None):  27         self.userName=userName  28         self.passWord=passWord  29 
 30     #update方法修改用戶名和登錄密碼
 31     def update(self,userName,passWord):  32         self.userName=userName  33         self.passWord=passWord  34 
 35     #__repr__()方法定義對象打印格式
 36     def __repr__(self):  37         return 'userName=%s\tpassWord=%s'%(self.userName,self.passWord)  38 
 39 ##函數showAll()顯示當前已注冊用戶信息########################
 40 def showAll():  41     global userList  42     if len(userList)==0:  43         print('\t當前無注冊用戶')  44     else:  45         print('\t當前已注冊用戶信息如下:')  46         n=0  47         for x in userList:  48             n+=1
 49             print('\t%s. '%n,x)  50         input('\n\t按Enter鍵繼續...\n')  51 
 52 
 53 ##函數check_update()執行查找、修改或刪除操作######################
 54 def check_update():  55     global userList  56     userName=input('\t請輸入要查找的用戶名:')  57     index=find(userName)  58     if index==-1:  59         print('\t%s不存在!'%userName)  60     else:  61         #用戶名已注冊,執行修改或刪除操作
 62         print('\t%s 已經注冊!'%userName)  63         print('\t請選擇操作:')  64         print('\t 1.修改用戶')  65         print('\t 2.刪除用戶')  66         op=input('\t請輸入序號選擇對應操作:')  67         if op=='2':  68             #刪除用戶
 69             del userList[index]  70             print('\n\t 已成功刪除用戶!')  71         else:  72             #修改用戶信息
 73             userName=input('\t請輸入新的用戶名:')  74             if userName=='':  75                 print('\t用戶名輸入無效!')  76             else:  77                 #檢查是否已存在同名的注冊用戶
 78                 if find(userName)>-1:  79                     print('\t你輸入的用戶名已經使用!')  80                 else:  81                     passWord=input('\t請輸入新用戶登錄密碼:')  82                     if passWord=='':  83                         print('\t登錄密碼輸入無效!')  84                     else:  85  userList[index].update(userName,passWord)  86                         print('\n\t已成功修改用戶!')  87     input('\n\t按Enter鍵繼續...\n')  88 
 89 
 90 ##函數addUser()添加新用戶########################
 91 def addUser():  92     global userList  93     userName=input('\t請輸入用戶名:')  94     if userName=='':  95         print('\t用戶名輸入無效!')  96     else:  97         #檢查是否已存在同名的注冊用戶
 98         if find(userName)>-1:  99             print('您輸入的用戶名已經使用,請重新添加用戶!') 100         else: 101             passWord=input('\t請輸入新用戶登錄密碼:') 102             if passWord=='': 103                 print('\t登錄密碼輸入無效!') 104             else: 105  userList.append(user(userName,passWord)) 106                 print('\t已成功添加用戶!') 107     input('\n\t按Enter鍵繼續........') 108 
109 ##函數find(namekey)查找是否存在用戶名為namekey的注冊用戶
110 def find(namekey): 111     global userList 112     #如果注冊用戶列表userList中存在namekey的用戶,則返回位置,否則返回-1
113     n=-1
114     for x in userList: 115         n+=1
116         if x.userName==namekey: 117             break
118     else: 119         n=-1
120     return n 121 
122 ##函數save()將當前用戶信息寫入文件永久保存
123 def save(): 124     global userList 125     #將用戶寫入文件永久保存
126     myfile=open(r'userdata.bin','wb') 127     global userList 128  dump(userList,myfile) 129  myfile.close() 130     print('\t已成功保存用戶信息') 131     input('\n\t按Enter鍵繼續......') 132 
133 ##程序啟動時,載入文件中的用戶數據
134 myfile=open(r'userdata.bin','rb') 135 x=myfile.read(1) 136 if x==b'': 137     userList=list() 138 else: 139  myfile.seek(0) 140     userList=load(myfile) 141 myfile.close() 142 
143 #以死循環顯示系統操作菜單,直到選擇退出系統
144 while True: 145     print('用戶注冊信息管理系統') 146     print('\t1. 顯示全部已注冊用戶') 147     print('\t2. 查找/修改/刪除用戶信息') 148     print('\t3. 添加新用戶') 149     print('\t4. 保存用戶數據') 150     print('\t5. 退出系統') 151     no=input('請輸入序號選擇對應菜單:') 152     if no=='1': 153  showAll() 154     elif no=='2': 155  check_update() 156     elif no=='3': 157  addUser() 158     elif no=='4': 159  save() 160     elif no=='5': 161         print('謝謝使用,系統已退出') 162         break

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM