第2模塊第1章35函數(shù)—練習(xí)題

修改個人信息程序

在一個文件里存多個人的個人信息,如以下

username password? age position department

alex? ? abc123? ? 24? Engineer? IT

rain? ? df2@432? ? 25? Teacher? Teching

....

1.輸入用戶名密碼,正確后登錄系統(tǒng) ,打印

1. 修改個人信息

2. 打印個人信息

3. 修改密碼

2.每個選項(xiàng)寫一個方法

3.登錄時輸錯3次退出程序










import os


# 負(fù)責(zé)登錄的函數(shù)

def login():

????dic = {}

????fo =open("staff.txt","rt",encoding="UTF-8")

????for linein fo:

????inf = line.split(",")

????dic[inf[0]] = line.split(",")

????print(dic)

????count =0

? ? username =None

? ? last_name =None

? ? flag =True

? ? while count <3:

username =input("請輸入用戶名:")

password =input("請輸入密碼:")

if last_name != usernameand count >0:

flag =False

? ? ? ? last_name = username

count +=1

? ? ? ? if usernamenot in dic:

print("用戶名不存在")

continue

? ? ? ? elif dic[username][-1] =="1\n":

print("用戶%s被鎖定,禁止登陸!" % username)

exit()

else:

if dic[username][1] == password:

print("welcome??!")

break

? ? ? ? ? ? else:

print("密碼輸入錯誤??!")

continue

? ? else:

if flagis True:

(dic[username][-1]) ="1\n"

? ? ? ? ? ? print("用戶" + username +"被鎖定,禁止登陸!請聯(lián)系管理員!")

fo =open("staff.txt","wt",encoding="UTF-8")

for keyin dic:

fo.writelines(",".join(dic[key]))

fo.close()

exit()

return dic[username]

# 打印用信息

def print_p(user_info):

print("Name:", user_info[2])

print("Age:", user_info[3])

print("Job:", user_info[4])

print("Dept:", user_info[4])

print("Phone:", user_info[6])

# 為將修改后的信息寫回文件的函數(shù),做準(zhǔn)備

def your(old_str, command, user_info):

print("current value>:", user_info[command])

user_info[command] =input("new value>:")

your_script(old_str,",".join(user_info),"staff")

# 將修改后的信息寫回文件的函數(shù)

def your_script(old_str, new_str, filename):

filename = filename +".txt"

? ? f_new_name ="new%s" % filename

f =open(filename,"r",encoding="utf-8")

f_new =open(f_new_name,"w",encoding="utf-8")

for linein f:

if old_strin line:

line = line.replace(old_str, new_str,1)

f_new.write(line)

f.close()

f_new.close()

os.remove(filename)

os.rename(f_new_name, filename)

# 修改用戶信息的函數(shù)

def modify(user_info):

print("person data:", user_info)

print("2.Name:", user_info[2])

print("3.Age:", user_info[3])

print("4.Job:", user_info[4])

print("5.Dept:", user_info[4])

print("6.Phone:", user_info[6])

command =eval(input("[select column id to change]:"))

old_str =",".join(user_info)

if command ==2:

your(old_str, command, user_info)

elif command ==3:

your(old_str, command, user_info)

elif command ==4:

your(old_str, command, user_info)

elif command ==5:

your(old_str, command, user_info)

elif command ==6:

your(old_str, command, user_info)

# 修改密碼的函數(shù)

def modify_password(user_info):

command1 =input("請輸入新密碼>>>")

command2 =input("請?jiān)俅屋斎胄旅艽a>>>")

if command1 == command2:

old_str =",".join(user_info)

user_info[1] = command1

your_script(old_str,",".join(user_info),"staff")

else:

print("兩次密碼不同!")

# 主函數(shù)

def main():

user_info = login()

while True:

print("""1. 打印個人信息

2. 修改個人信息

3. 修改密碼""")

command =input(">>>:")

if command =="1":

print_p(user_info)

elif command =="2":

modify(user_info)

elif command =="3":

modify_password(user_info)

elif command =="q":

quit()

else:

print("命令錯誤!無法理解")

main()



基本完成功能,待美化修改

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容