马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我在别人的启发下写的,他的比较简单,我帮忙写的更加深入
import os
import pickle
if not os.path.exists('E:/通讯.pkl'):
with open('E:/通讯.pkl', 'wb') as f:
pickle.dump({},f)
else:
with open('E:/通讯.pkl', 'rb') as f:
book = pickle.load(f)
def phone(a):
length = len(a)
while (a.isspace() or length == 0):#排除空格和没输入密码
a = input('电话不能空,请重新输入电话:')
length = len(a)
if length == 11:
flag = 0
else:
flag = 1
for i in a:
if i.isdigit():
flag1 = 0
else:
flag1 = 1
break
while 1:
if flag == 0 and flag1 == 0:
break
else:
a = input('输入不合法,请输入正确的电话:')
phone(a)
break
return a
def amend(book):
s = 1
while s:
nam = input("请输入要修改的用户名:")
if nam not in book:
nam = input('用户不存在,请重新输入要修改的用户名:')
try:
choice = int(input("请问修改哪一项(按1性别,按2电话号码,按3家庭住址,按其他数字退出):"))
except ValueError as reason:
print('输入出错啦T_T\n错误原因是:' + str(reason))
if choice in [1, 2, 3]:
book[nam][choice_list[choice]] = input("请输入{}的修改值:".format(choice_list[choice]))
s = input("是否继续修改,需要按0,不需要随意:")
if s == '0':
s = 1
continue
else:
s = 0
else:
print('本轮结束')
s = 0
print("-----修改完毕-----")
with open('E:/通讯.pkl', 'wb') as f:
pickle.dump(book, f)
def entering(book):
print("-----录入模式-----")
name = input("请输入用户名:")
if name in book: # 穿插修改部分
try:
name = int(input("电话簿中已有{}用户,是否修改?(是1/否2)".format(name)))
except ValueError as reason:
print('输入出错啦T_T\n错误原因是:' + str(reason))
if name != 1:
print("已结束本轮")
elif name == 1:
amend(book)
else:
try:
gender = int(input("{}性别(男1/女2/未知3):".format(name)))
except ValueError as reason:
print('输入出错啦T_T\n错误原因是:' + str(reason))
if gender == 1:
gender = "男"
elif gender == 2:
gender = "女"
else:
gender = "未知"
phonenum = input("{}的电话号码:".format(name))
phone(phonenum)
home = input("{}的家庭住址(若未知按1):".format(name))
if home == '1':
home = '未知'
book[name] = {'用户名': name, '性别': gender, '电话号码': phonenum, '家庭住址': home}
print("-----录入完毕-----")
with open('E:/通讯.pkl', 'wb') as f:
pickle.dump(book, f)
return book
'''豆嘉木电话簿'''
print("-------欢迎使用豆嘉木电话簿-------")
choice_list = [0, '性别', '电话号码', '家庭住址']
while True:
p = int(input("按1是录入,按2是查询按,按3是删除,按其他数字退出:")) # 录入部分
if p == 1:
entering(book)
elif p == 2:
print("-----查询模式-----")
try:
check = input("请输入查询对象的名称:")
except TypeError:
print('输入错误')
if check not in book:
while 1:
check = int(input("%s不在电话簿中,请问是否增加联系人(是1/否2):"%check))
if check == 1:
entering(book)
break
else:
break
else:
print('用户名:%s \n性别:%s \n电话号码:%s \n家庭住址:%s'%(book[check]['用户名'], book[check]['性别'],
book[check]['电话号码'], book[check]['家庭住址']))
elif p == 3:
print("-----删除模式-----")
delmem = input("需要删除哪一个用户(按数字退出):")
if delmem.isdigit():
print("-----退出删除模式-----")
elif delmem not in book:
delmem = int(input("%s不在电话簿中,请问是否增加联系人(是1/否2):" %delmem))
if delmem == 1:
entering(book)
else:
del book[delmem]
with open('E:/通讯.pkl', 'wb') as f:
pickle.dump(book, f)
print("-----删除完毕-----")
else:
print("-----程序终止-----")
break
|