|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
初学者,看不懂哪里出了问题,大佬帮忙看一下,解决问题的10鱼币
程序目的:电话簿,有录入(其中包含修改),打印用户列表,查询,删除四大板块
出现的两出问题:
1,第51行,系统没有报错,但只有我的电话号码位数为8时才会正常运行,其它各种情况(包括11位)都会打印后面的"电话号码的长度为8或11位,请从头重新输入!!!"
2,第70行,系统报错,具体如下图
第二个问题的图片
完整代码如下:
- '''豆嘉木电话簿'''
- print("-------欢迎使用豆嘉木电话簿-------")
- book = {}
- man = "男"
- woman = "女"
- choicelist = [0,"性别","电话号码","家庭住址"]
- while True:
- p = int(input("录入按1,打印用户列表按2,查询按3,删除按4,退出按0:")) #录入部分
- if p != 1 and 2 and 3 and 0:
- print("请按要求输入!!!")
- elif p == 1:
- print("-----录入模式-----")
- name = input("请输入用户名:")
- if name in book: #穿插修改部分
- o = int(input("电话簿中已有{}用户,是否修改?(是1/否2)".format(name)))
- if 0 == 2:
- print("已结束本轮")
- continue
- elif o == 1:
- while s == 1:
- choice = int(input("请问修改哪一项(性别按1,电话号码按2,家庭住址按3,退出按0):"))
- if choice != 1 or 2 or 3 or 0:
- print("请按要求输入!!!")
- s = 1
- elif choice == 0:
- print("已结束本轮循环")
- s = 0
- else:
- book[name][choicelist[choice]] = input("请输入{}的修改值:".format(choicelist[choice]))
- s = input("是否继续修改,不需要按0,需要随意:")
- if s == '0':
- s = 0
- continue
- else:
- s = 1
- print("-----修改完毕-----")
- continue
- gender = int(input("{}性别(男1/女2/未知3):".format(name)))
- if gender != 1 and 2 and 3:
- print("请正确输入!!!")
- continue
- elif gender == 3:
- gender = "未知"
- elif gender == 2:
- gender = "女"
- else:
- gender = "男"
- phonenum = input("{}的电话号码:".format(name))
- if len(phonenum) != 8 and 11:
- print("电话号码的长度为8或11位,请从头重新输入!!!")
- continue
- home = input("{}的家庭住址(若未知按1):".format(name))
- if home == 1:
- home = "未知"
- book = book.update({name:{"用户名:":name,"性别:":gender,"电话号码:":phonenum,"家庭住址":home}})
- print("-----录入完毕-----")
- elif p == 2:
- print("-----打印模式-----")
- print("电话簿中的用户共有{}位,分别为".format(len(book)))
- menberslen = len(book)
- members = list(book.keys())
- for i in range(0,len(book)):
- print(members[i])
- print("-----打印完毕-----")
- elif p == 3:
- print("-----查询模式-----")
- check = input("请输入查询对象的名称:")
- if check not in book:
- print("{}不在电话簿中,可先打印用户列表,再重新输入".format(delmem))
- else:
- print("用户名:{} \n性别:{} \n电话号码:{} \n家庭住址:{}".format(book[check]['用户名'],book[check]['性别'],
- book[check]['电话号码'],book[check]['家庭住址']))
- elif p == 4:
- print("-----删除模式-----")
- delmem = input("需要删除哪一个用户(退出按0):")
- if delmem == 0:
- print("-----退出删除模式-----")
- elif delmem not in book:
- print("{}不在电话簿中,可先打印用户列表,再重新输入".format(delmem))
- continue
- else:
- del book[delmem]
- print("-----删除完毕-----")
- elif p == 0:
- print("-----程序终止-----")
- break
复制代码
兄弟,你的代码,我给你优化了
你自己看看
- 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 entering(book):
- print("-----录入模式-----")
- name = input("请输入用户名:")
- if name in book: # 穿插修改部分
- name = int(input("电话簿中已有{}用户,是否修改?(是1/否2)".format(name)))
- if name != 1:
- print("已结束本轮")
- elif name == 1:
- s = 1
- while s:
- nam = input("请输入要修改的用户名:")
- if nam not in book:
- nam = input('用户不存在,请重新输入要修改的用户名:')
- choice = int(input("请问修改哪一项(按1性别,按2电话号码,按3家庭住址,按0退出):"))
- if choice not in [1, 2, 3, 0]:
- print("请按要求输入!!!")
- s = 1
- continue
- elif choice == 0:
- print("已结束本轮循环")
- s = 0
- else:
- book[nam][choice_list[choice]] = input("请输入{}的修改值:".format(choice_list[choice]))
- s = input("是否继续修改,需要按0,不需要随意:")
- if s == '0':
- s = 1
- continue
- else:
- s = 0
- print("-----修改完毕-----")
- with open('E:/通讯.pkl', 'wb') as f:
- pickle.dump(book, f)
- else:
- gender = int(input("{}性别(男1/女2/未知3):".format(name)))
- while gender not in [1, 2, 3]:
- print("请正确输入!!!")
- gender = int(input("{}性别(男1/女2/未知3):".format(name)))
- if gender in [1, 2, 3]:
- break
- if gender == 3:
- gender = "未知"
- elif gender == 2:
- gender = "女"
- else:
- gender = "男"
- phonenum = input("{}的电话号码:".format(name))
- while len(phonenum) != 11:
- print("电话号码的长度为8或11位,请重新输入!!!")
- phonenum = input("{}的电话号码:".format(name))
- if len(phonenum) == 11:
- break
- 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是删除,按0退出:")) # 录入部分
- if p not in [1, 2, 3, 0]:
- print("请按要求输入!!!")
- elif p == 1:
- entering(book)
- elif p == 2:
- print("-----查询模式-----")
- check = input("请输入查询对象的名称:")
- 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("需要删除哪一个用户(退出按0):")
- if delmem == 0:
- 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("-----删除完毕-----")
- elif p == 0:
- print("-----程序终止-----")
- break
复制代码
|
|