通讯系统制作python

简单通讯录系统的制作
代码如下

import os
print("        通讯录系统            ")
print("     1.添加通讯录成员         ")
print("     2.查询通讯录成员         ")
print("     3.修改通讯录成员         ")
print("     4.删除通讯录成员         ")
print("     5.退出程序               ")
print("                             ")
people_list=[]
def add():
    name=input("请输入添加的名字")
    age=input("请输入添加的年龄")
    sex=input("请输入添加的性别")
    phone=input("请输入添加的电话")
    people=[name,age,sex,phone]
    people_list.append(people)
def chaxun():
    for i in range(0,len(people_list)):
        people=people_list[i]
        name=people[0]
        age=people[1]
        sex=people[2]
        phone=people[3]
        print("名字:%s 年龄:%s 性别:%s 电话:%s"%(name,age,sex,phone))
def xiugai():
    c=str(input("请输入需要修改的人的名字:"))
    cc=[]
    for i in people_list:
        cc.append(i[0])
    if c not in cc:
        print("没有此人,长点心吧!重新选择")
    else:
        name = filter(lambda x: c == x[0], people_list)
        xx = list(name)[0]
        people=people_list[people_list.index(xx)]
        newname=input("输入修改后的名字:")
        newage = input("输入修改后的年龄:")
        newsex = input("输入修改后的性别:")
        newphone= input("输入修改后的电话号码:")
        people[0]=newname
        people[1]=newage
        people[2]=newsex
        people[3]=newphone
def delte():
    q=str(input("请输入需要删除的人的名字:"))
    cc = []
    for i in people_list:
        cc.append(i[0])
    if q not in cc:
        print("没有此人,长点心吧!重新选择")
    else:
        name = filter(lambda x: q == x[0], people_list)
        xx = list(name)[0]
        if q==xx[0]:
            people_list.remove(xx)
            print("删除成功")
def save():
    f=open("E:\\python.txt","w")
    for people in people_list:
        s="#".join(people)
        f.write(s)
        f.write("\n")
    f.close()
xx=[]
def read():
    x=os.path.exists("E:\\python.txt")
    if x==True:
        f=open("E:\\python.txt","r")
        while 1:
            C=f.readline()
            if not C:
                break
            c=C.strip("\n")
            list=c.split("#")
            people_list.append(list)
        f.close()
read()
while True:
    x=int(input("请选择:"))
    while x not in range(1,6):
        x=int(input("请重新选择,您的选择错误"))
    if x==1:
        add()
    elif x==2:
        chaxun()
    elif x==3:
        xiugai()
    elif x==4:
        delte()
    else:
        save()
        print("保存成功,谢谢您的使用")
        break

效果如下
通讯录简介:
通讯录包括:1.添加 2.查询 3.修改 4.删除 四个选项
1.添加
代码如下:

def add():
    name=input("请输入添加的名字")
    age=input("请输入添加的年龄")
    sex=input("请输入添加的性别")
    phone=input("请输入添加的电话")
    people=[name,age,sex,phone]
    people_list.append(people)

创建一个小列表,将一个人的数据保存在其中,然后将所有的小列表保存在一个大列表里面,方便选择,与判断。如上,添加姓名,年龄,性别,电话。保存在people列表里面,然后将people列表保存在people_list列表里。
2.查询
代码如下:

def chaxun():
    for i in range(0,len(people_list)):
        people=people_list[i]
        name=people[0]
        age=people[1]
        sex=people[2]
        phone=people[3]
        print("名字:%s 年龄:%s 性别:%s 电话:%s"%(name,age,sex,phone))

查询呢,和添加一样,查询出people_list列表里面的所有people列表,再输出出people列表里面的所有元素。
3.修改:

def xiugai():
    c=str(input("请输入需要修改的人的名字:"))
    cc=[]
    for i in people_list:
        cc.append(i[0])
    if c not in cc:
        print("没有此人,长点心吧!重新选择")
    else:
        name = filter(lambda x: c == x[0], people_list)
        xx = list(name)[0]
        people=people_list[people_list.index(xx)]
        newname=input("输入修改后的名字:")
        newage = input("输入修改后的年龄:")
        newsex = input("输入修改后的性别:")
        newphone= input("输入修改后的电话号码:")
        people[0]=newname
        people[1]=newage
        people[2]=newsex
        people[3]=newphone

修改,需要提供修改者的信息,然后进行修改,此处我采用,输入的str字符串是否与people_list列表里的people列表的第一个字符串是否相同,如果不同就输出,没有这个人。相同的话就输出其people列表。
4.删除:

def delte():
    q=str(input("请输入需要删除的人的名字:"))
    cc = []
    for i in people_list:
        cc.append(i[0])
    if q not in cc:
        print("没有此人,长点心吧!重新选择")
    else:
        name = filter(lambda x: q == x[0], people_list)
        xx = list(name)[0]
        if q==xx[0]:
            people_list.remove(xx)
            print("删除成功")

删除手法与修改相同,找到输入的people列表,对比,相同就删除此列表,不同就要求重新输出。

def save():
    f=open("E:\\python.txt","w")
    for people in people_list:
        s="#".join(people)
        f.write(s)
        f.write("\n")
    f.close()
xx=[]
def read():
    x=os.path.exists("E:\\python.txt")
    if x==True:
        f=open("E:\\python.txt","r")
        while 1:
            C=f.readline()
            if not C:
                break
            c=C.strip("\n")
            list=c.split("#")
            people_list.append(list)
        f.close()

上述为保存文件与查看文件,保存文件时,将people_list列表里面的people列表里面的元素用#分割,然后每一个people列表换行保存。查看时,查看是否有此文件,如果有,就进行读取,每一行的数据,然后以#为分割,进行列表输出,然后保存在people_list里面。

此时我们关闭之前的程序。
再次打开。
就可以查询文本内容

猜你喜欢

转载自blog.csdn.net/qq_40594554/article/details/82229180