编写一个简单的通讯录python(编写一个简单的通讯录管理程序)

http://www.itjxue.com  2023-02-17 18:01  来源:未知  点击次数: 

急需一个python通讯录 简单的就行 可以 增加 删除 编辑 搜索 显示 联系人 期末作业要用

要求提的不是很明确,按照最简单的功能来做了。

只记录姓名和电话号码,没有做号码检验。也没有做保存功能。

import?sys

last_id?=?0

class?Person:

????def?__init__(self,?name,?phone):

????????self.name?=?name

????????self.phone?=?phone

????????global?last_id

????????last_id?+=?1

????????self.id?=?last_id

?

????def?match(self,?filter):

????????return?filter?in?self.name?or?filter?in?self.phone

?

class?AddressBook:

????def?__init__(self):

????????self.people?=?[]

?

????def?new_person(self,?name,?phone=''):

????????self.people.append(Person(name,?phone))

?

????def?find_person(self,?id):

????????for?person?in?self.people:

????????????if??str(id)?==?str(person.id):

????????????????return?person

????????return?None

?

????def?modify_person(self,?id,?name,?phone):

????????person?=?self.find_person(id)

????????if?person:

????????????person.name?=?name

????????????person.phone?=?phone

????????else:

????????????print?"person:",?id,?"?not?found"

?

????def?delete_person(self,?id):

????????person?=?self.find_person(id)

????????if?person:

????????????self.people.remove(person)

?

????def??search(self,?filter):

????????return?[person?for?person?in?self.people?if?person.match(filter)]

?

class?Menu:

????def?__init__(self):

????????self.address_book?=?AddressBook()

????????self.choices?=?{

????????????"1":?self.show_people,

????????????"2":?self.add_person,

????????????"3":?self.remove_person,

????????????"4":?self.modify_person,

????????????"5":?self.search_person,

????????????"6":?self.quit

????????}

?

????def?display_menu(self):

????????print("""

Address?Book?Menu

1.?Show?all?People

2.?Add?Person

3.?Remove?Person

4.?Modify?Person

5.?Search?Person

6.?Quit

""")

?

????def?run(self):

????????while?True:

????????????self.display_menu()

????????????choice?=?raw_input("Enter?an?option:?")

????????????action?=?self.choices.get(choice)

????????????if?action:

????????????????action()

????????????else:

????????????????print("{0}?is?not?a?valid?choice".format(choice))

?

????def?show_people(self,?people=None):

????????if?not?people:

????????????people?=?self.address_book.people

????????for?person?in?people:

????????????print("{0}:?{1}?{2}".format(person.id,?person.name,?person.phone))

?

????def?search_person(self):

????????filter?=?raw_input("search?for:?")

????????people?=?self.address_book.search(filter)

????????self.show_people(people)

?

????def?add_person(self):

????????name?=?raw_input("Enter?a?name:?")

????????phone?=?raw_input("Enter?his/her?phone?number:?")

????????self.address_book.new_person(name,?phone)

????????print("{0}?has?been?added.".format(name))

?

????def?remove_person(self):

????????id?=?raw_input("Enter?his/her?id:?")

????????if?id:

????????????self.address_book.delete_person(id)

?

????def?modify_person(self):

????????id?=?raw_input("Enter?a?person?id:?")

????????name?=?raw_input("Enter?his/her?name:?")

????????phone?=?raw_input("Enter?his/her?phone:?")

????????self.address_book.modify_person(id,?name,?phone)

?

????def?quit(self):

????????print("Thank?you?for?using?your?address?book?today.")

????????sys.exit(0)

?

Menu().run()

python简单的好友通讯录管理程序怎么做

friend={ '小明':['001', '广州'],'小红':['002','深圳'],'小王':['003','北京']}

model=input("model:")

if model=='1':

print("好友添加:",end='')

new_friend=input("newfriend:name,number,address")

friend_n=list(new_friend.split(','))

friend[friend_n[0]] =friend_n[1:3] # 添加

# friend4=dict.fromkeys([friend_n[0]],friend_n[1:3])

print("friend: ", friend)

elif model=='2':

print("好友删除:",end='')

new_friend = input("name")

# del friend[ new_friend] # 删除键是'Name'的条目

try:

friend.pop(new_friend)

print("friend: ", friend)

except:

print("查无此人")

elif model=='3':

print("好友修改:", end='')

new_friend = input("name")

friend[new_friend]=list(input("number,address").split(','))

print("friend: ", friend)

else:

print("好友查询:", end='')

new_friend = input("name")

try:

print(new_friend,':', friend[new_friend])

except:

print("查无此人")

以上程序包含了整个流程,有删赠改查功能,还有利用异常处理的处理过程。

python:编写一个通讯录管理程序,实现通讯录文件建立,读取,添加记录,查找记录的功能。

可以将数据存储在列表里

然后再将列表保存到文件里

通过对列表的操作动态更改存储在文件里的内容

如果有文件的话程序启动时从文件里加载数据保存到列表即可

如果代码不想自己写的话

我可以有偿代劳

python用单链表写一个通讯录,包括添加,删除(可恢复),查找等基本功能

///////////list3.c实现链表的插入删除查找

#include

#include

#include

typedef

struct

LNode

//////////定义数据结构体

{

int

num;

char

name[20];

struct

LNode*

next;

}*Link;

///////////定义一个指针类型

typedef

struct

{

Link

head,tail;

int

len;

}LinkList;

LinkList

*gList;

void

MenuInfo();

void

InputData(LinkList

*mList);

void

OutputData(LinkList

*mList);

void

InsertData(LinkList

*mList,int

n);

Link

SearchNode(LinkList

*mList,int

n);

void

DeleteData(LinkList

*mList,int

n);

void

main()

{

int

_choice;

int

_quit=0;

int

n=0;

gList=(LinkList

*)malloc(sizeof(LinkList));

gList-head=gList-tail=NULL;

do

{

MenuInfo();

scanf("%d",_choice);

switch(_choice)

(责任编辑:IT教学网)

更多

推荐站内动态文章