编写程序,生词本。
def sayHello():print("=" * 20 + '\n' + '欢迎使用生词本\n'+ '1.查看生词本\n'+ '2.背单词\n'+ '3.添加新单词\n'+ '4.删除单词\n'+ '5.清空生词本\n'+ '6.退出生词本\n'+ '=' * 20 + '\n')def addW(data):word = input("请输入新单词:")trans = input("请输入单词翻译:")data[word] = transprint("单词添加成功")print(f"{word}:{data[word]}")def showW(data):if len(data) == 0:print("恭喜你,没有生单词!\n")else:for i in data:print(f"{i}:{data[i]}")def bei(data):if (len(data.keys()) != 0):for i, v in data.items():print(i)v2 = input("请输入单词翻译(q退出):")if (v2 == v):print("正确!")else:if (v2 == "q"):print("退出成功")else:print(f'遗憾!正确答案是:{v}\n')print("背完了!再来一遍吧")else:print("没有单词")def isNoneW(data):if len(data.keys()) == 0:return Truereturn Falsedef delW(data):if isNoneW(data):print("没有单词哦")else:word = input("请输入要删除的单词")if (word in data.keys()):data.pop(word)print("删除成功")else:print("没有这个单词哦")def defAll(data):data.clear()print('清空成功')if __name__ == "__main__":sayHello()data = {}num = int(input("请输入功能编号:\n"))while (num != 6):if (num == 1):showW(data)elif (num == 2):bei(data)elif (num == 3):addW(data)elif (num == 4):delW(data)elif (num == 5):defAll(data)else:breaknum = int(input("请输入功能编号:\n"))
2.模拟实现手机通讯录
def sayHello():print("="*20+'\n'+'欢迎使用通讯录\n'+'1.添加联系人\n'+'2.查看通讯录\n'+'3.删除联系人\n'+'4.修改联系人\n'+'5.查找联系人\n'+'6.退出\n'+'='*20+'\n')
def add(data):name=input("请输入联系人的姓名:")phone = input("请输入联系人的手机号:")mail = input("请输入联系人的邮箱:")address = input("请输入联系人的地址:")data[name]={'phone':phone,'mail':mail,'address':address}print(f"保存成功:{name}:{data[name]}")
def show(data,*name):if(len(name)!=0):print(f"查找结果为:{name}:{data[name[0]]}")else:if(len(data.keys())==0):print("没有联系人")else:print(f"所有联系人:\n")for i in data.keys():print(f"{i}\t{data[i]}")print("="*20)
def delD(data):name=input("请输入删除人姓名:")if name in data.keys():data.pop(name)print("删除成功")else:print("删除失败,用户不存在")
def change(data):if(len(data.keys())!=0):name = input('请输入修改人姓名:')if(name in data.keys()):inType=input("请输入要修改的选项:【name,phone,mail,address】")if(inType=='name'):newname=input("请输入要修改后的姓名:")if(newname in data.keys()):print("该用户名存在了,修改失败")else:temp=data[name]data.pop(name)data[newname]=tempprint('修改成功')elif inType in ['name','phone','mail','address']:new=input("请输入要修改的值")data[name][inType]=newelse:print("该人不存在,请重新输入")name = input('请输入修改人姓名:')change(name)else:print("现在还没有联系人")
if __name__=="__main__":sayHello()data = {}num=int(input("请输入功能序号:\n"))while(num!=6):if(num==1):add(data)elif(num==2):show(data)elif(num==3):delD(data)elif(num==4):change(data)elif(num==5):name=input("请输入要查找的人:")show(data,name)else:num=6num=int(input("请输入功能序号:"))