老男孩Python全栈

第2模块第1章35函数—练习题

2019-03-12  本文已影响7人  飞鸟在笼

修改个人信息程序

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

username password  age position department

alex    abc123    24  Engineer  IT

rain    df2@432    25  Teacher  Teching

....

1.输入用户名密码,正确后登录系统 ,打印

1. 修改个人信息

2. 打印个人信息

3. 修改密码

2.每个选项写一个方法

3.登录时输错3次退出程序

import os

# 负责登录的函数

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 +"被锁定,禁止登陆!请联系管理员!")

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])

# 为将修改后的信息写回文件的函数,做准备

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")

# 将修改后的信息写回文件的函数

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)

# 修改用户信息的函数

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)

# 修改密码的函数

def modify_password(user_info):

command1 =input("请输入新密码>>>")

command2 =input("请再次输入新密码>>>")

if command1 == command2:

old_str =",".join(user_info)

user_info[1] = command1

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

else:

print("两次密码不同!")

# 主函数

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()

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

上一篇 下一篇

猜你喜欢

热点阅读