Python while错误
Main.py:程序代码:
from exAdd import AddInformation from exFind import FindInformation print("|---欢迎进入Python通讯录---|") print("|-----1.查询联系人资料-----|") print("|------2.添加新联系人------|") print("|-------3.退出通讯录-------|") a=int(input("输入数字执行相应功能:") while True: #SyntaxError:invalid syntax if a==1: AddInformation elif a==2: FindInformation else: break
exAdd.py:
程序代码:
def AddInformation(): try: file=open("temp.txt","r+") pb=eval(file.readline()) file.seek(0) file.truncate() except: file=open("temp.txt","w") pb={} while True: name=input("请输入联系人姓名:(输入exit退出输入)") if name=="exit": break phone=input("请输入联系人电话:") pb[name]=phone strpb=str(pb) file.write(strpb) file.close
exFind.py:
程序代码:
def FindInformation(): try: file=open("temp.txt","r") pb=eval(file.readline()) name=input("请输入联系人姓名(输入exit退出):") if name=="exit": return if name in pb: print("联系人",str(name),"的电话号码是:",pb[name]) else: print("联系人不存在!") file.close() except: print("查询出错!")