每日學點python之十一(XX管理系統)
阿新 • • 發佈:2019-01-08
任務描述:
1.實現登入、註冊、退出。
2.檔案操作,實現增刪改查。
3.必要的容錯處理。
## # @file book_system.py # @brief # @author SongQiang # @version 1.0 # @date 2018-12-25 import login_register def start_page(): print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++') print(' ' * 27 + '1.登 錄') print(' ' * 27 + '2.注 冊') print(' ' * 27 + '3.退 出') print('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++') def verfication(): while True: start_page() user_input=input("請輸入你想選擇的功能") if user_input.isdigit(): if user_input=='1': login_register.login() elif user_input=='2': login_register.register() elif user_input=='3': exit(0) else: print("沒有此功能,請重新輸入") verfication() else: print("請輸入正確的數字") verfication() def main_system(): verfication() if __name__=='__main__': main_system()
## # @file login_register.py # @brief # @author SongQiang # @version 1.0 # @date 2019-01-02 import book_system import os import file_operation def file_exit(): #獲取當前目錄 path=os.getcwd() if os.path.exists('%s/admin.txt'%path): pass else: with open('./admin.txt','w') as f: print('使用者不存在,請註冊') register() def register(): file_exit() user_name=input("請輸入你的姓名") if not user_name: print("姓名不能為空,請重新輸入") register() if file_operation.user_repetition(user_name): print("使用者已經存在,請重新輸入") book_system.main_system() else: user_pass1=input("請輸入密碼:") user_pass2=input("請重新輸入密碼") if user_pass1 !=user_pass2: print('兩次密碼不一致,請重新輸入!!!') register() elif len(user_pass1)<3: print('密碼長度過低,請至少輸入3位密碼') register() else: print("註冊成功") path1='./%s.txt'%user_name with open(path1,'w') as f: print('建立管理檔案成功') file_operation.add_admin(user_name,user_pass1) def login(): file_exit() user_name=input('請輸入使用者名稱') user_pass=input('請輸入密碼') file_operation.login_check(user_name,user_pass)
## # @file file_operation.py # @brief # @author SongQiang # @version 1.0 # @date 2019-01-02 import book_system import login_register import operation def add_id(filename): #將資料儲存到列表裡 path='./%s.txt'%filename id_list=[] with open(path,'r',encoding='utf-8') as f: while True: #擷取左邊的空格或指定字元 a=f.readline().lstrip() #print(len(a)) if not a: break else: m=a.strip(',').strip().split(',') id_list.append(m[0]) return (len(id_list)+1) def add_student(admin,name,sex,hoby,tel): path='./%s.txt'%admin #自動編號 id =str(add_id(admin)) str1 ='%s,%s,%s,%s,%s'%(id,name,sex,hoby,tel) with open(path,'a') as f: f.write(str1+'\n') print('新增完成') #開始選擇 up_down(admin) def up_down(admin): print(' '*27+'1.返回上一層') print(' '*27+'2.退出重新登入') print(' '*27+'3.退出程式') user_input=input('請輸入你的選擇: ') if user_input.isdigit(): if user_input=='1': operation.choose_operation(admin) elif user_input=='2': book_system.verfication() elif user_input=='3': exit(0) else: print('沒有此功能') up_down(admin) else: print("請輸入正確的數字") up_down(admin) def all_info(filename): path='./%s.txt'%filename with open(path,'r',encoding='utf-8') as f: while True: a=f.readline() if not a: break m=a.strip(',').strip().split(',') if len(m)==17: continue print('學號:%s 姓名:%s 性別:%s 愛好:%s 電話:%s'%(m[0],m[1],m[2],m[3],m[4])) up_down(filename) def select_one_info(filename,info): path='./%s.txt'%filename with open(path,'r',encoding='utf-8') as f: while True: a=f.readline() if not a: break m=a.strip(',').strip().split(',') if info.isdigit(): if m[0]==info: print('學號:%s 姓名:%s 性別:%s 愛好:%s 電話:%s'%(m[0],m[1],m[2],m[3],m[4])) elif info.isalpha(): if m[1]==info: print('學號:%s 姓名:%s 性別:%s 愛好:%s 電話:%s'%(m[0],m[1],m[2],m[3],m[4])) else: print('請輸入正確的查詢內容,id或者姓名') select_one_info(filename,info) up_down(filename) def delete_info(id,admin): info_list=[] path='./%s.txt'%admin with open(path,'r',encoding='utf-8') as f: while True: a=f.readline() m=a.strip().split(',') if not a: break else: if m[0]==id: info_list.append('delete one person') #pass else: info_list.append(m) with open(path,'w') as f: for x in info_list: f.write(','.join(x)+'\n') up_down(admin) def update_info(filename,id,user_choose,user_update): info_list=[] path='./%s.txt'%filename with open(path,'r',encoding='utf-8') as f: while True: a=f.readline() m=a.strip().split(',') if not a: break else: if m[0]==id: m[int(user_choose)]=user_update info_list.append(m) else: info_list.append(m) with open(path,'w') as f: for x in info_list: f.write(','.join(x)+'\n') up_down(filename) def add_admin(username,userpass): with open('./admin.txt','a') as f: f.write('%s,%s\n'%(username,userpass)) book_system.main_system() def login_check(username,userpass): with open('./admin.txt','r',encoding='utf-8') as f: while True: #擷取字串左邊的空格 a=f.readline().lstrip() #判斷是否為空 if not a: break #strip() 不帶引數 預設清除兩邊的空白符 /n /r /t ' ' #帶引數 刪除字串最開頭和最結尾包含的字元 #print(a) m=a.strip(',').strip().split(',') if m[0]==username and m[1]==userpass: print('登陸成功') operation.choose_operation(username) break elif m[0]==username and m[1]!=userpass: print('密碼錯誤請重新輸入') # else: # print('使用者不存在') #login_register.login() def user_repetition(user): with open('./admin.txt','r',encoding='utf-8') as f: while True: a=f.readline() if not a: break m=a.strip(',').strip().split(',') #print(m[0]) if m[0]==user: return True else: return False
##
# @file operation.py
# @brief
# @author SongQiang
# @version 1.0
# @date 2019-01-02
import file_operation
import book_system
#增
def add_info(filename):
user_name=input('XX姓名:')
user_sex=input('性別:')
user_hoby=input('愛好:')
user_tel=input('電話:')
file_operation.add_student(filename,user_name,user_sex,user_hoby,user_tel)
#查詢
def select_info(filename):
user_input=input('1---查詢全部資訊 2---查詢部分資訊')
if user_input.isdigit():
if user_input=='1':
file_operation.all_info(filename)
elif user_input=='2':
user_input=input('請輸入姓名或者id:')
file_operation.select_one_info(filename,user_input)
else:
print('無此功能')
select_info(filename)
else:
print('請輸入正確的整數')
#刪除
def dele_info(filename):
user_input=input('請輸入id,忘記請輸入f:')
if user_input=='f':
select_info(filename)
elif user_input.isdigit():
file_operation.delete_info(user_input,filename)
else:
print('輸入錯誤請重新輸入')
dele_info(filename)
#改
def update_info(filename):
user_input=input('請輸入你的id,如果忘記id,請輸入f:')
if user_input=='f':
select_info(filename)
else:
user_choose=input('請輸入想修改的選項:1.姓名 2.性別 3.愛好 4.電話:')
use_update=input('請輸入修改的內容:')
file_operation.update_info(filename,user_input,user_choose,use_update)
def choose_operation(admin):
print(' '*27+'1.新增XX')
print(' '*27+'2.刪除XX')
print(' '*27+'3.檢視XX')
print(' '*27+'4.修改XX')
print(' '*27+'5.退出')
print(' '*27+'6.重新登陸')
while True:
user_input=input("請輸入你的操作")
if user_input.isdigit():
if user_input=='1':
add_info(admin)
elif user_input=='2':
dele_info(admin)
elif user_input=='3':
select_info(admin)
elif user_input=='4':
update_info(admin)
elif user_input=='5':
exit(0)
elif user_input=='6':
book_system.verfication()
break
else:
print('無此功能,請重新輸入')
else:
print("請輸入正確的數字")