python通讯录的录入与测试_python: 实现通讯录录入与查找的小脚本

本文介绍了使用Python编写的两个脚本,用于实现通讯录的录入和查找功能。通过定义classmates类并利用pickle模块存储数据,用户可以输入姓名、年龄、电话和邮箱来添加联系人。另外,提供了一个查找脚本,根据姓名查询联系人并选择显示特定信息。
摘要由CSDN通过智能技术生成

python: 实现通讯录录入与查找的小脚本

标签:python 通讯录 脚本

by 小威威

今天我用python写了两个小脚本,有了它们,我就可以实现通讯录的录入与查找,挺方便的。

直接上代码:

脚本1:实现通讯录的录入。

#!/usr/bin/python3

# Filename: address_list.py

# Call pickle module to store the message in the file

import pickle

# Define a class name classmates

class classmates:

def __init__(self, name, age, phonenumber, email_address):

self.name = name

self.age = age

self.phonenumber = phonenumber

self.email_address = email_address

# Through the while, we can put more group of data in it

while True:

name = input('Please enter your name:')

age = input('Please enter your age:')

phonenumber = input('Please enter your phonenumber:')

email_address = input('Please enter your email_address:')

lab = classmates(name, age, phonenumber, email_address)

info = {

'name': '%s' % lab.name,

'age': '%s' % lab.age,

'phonenumber': '%s' % lab.phonenumber,

'email_address': '%s' % lab.email_address

}

filename = '%s.txt' % name

f = open(filename, 'wb')

pickle.dump(info, f)

f.close()

# This is the condition of stopping the loop

choice = input('Do you want to continue,enter Y or N:')

if choice == 'N':

break

脚本二:实现通讯录的查找

#!/usr/bin/python3

# Filename: search_info.py

# Call the module of pickle to load the message import pickle # Through the message, we can achieve searching the info for more times while True: name = input('Please enter the person you want:') filename = '%s.txt' % name f = open(filename, 'rb') info = pickle.load(f) print ('Please enter the message you want,', end = '') message = input('age, phonenumber or email_address:') print(info[message]) choice = input('Do you want to print all its information,enter Y or N:') if choice == 'Y': for key, content in info.items(): print('%s : %s' %(key, content)) # This is the condition to stop the loop choice2 = input('Do you want to search others,enter Y or N:') if choice2 == 'N': break;

f.close()

以上内容皆为本人观点,欢迎大家提出批评和指导,我们一起探讨。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值