1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #!/usr/bin/python3 import os,sys os.chdir("./") #把读取文件的目录设置在当前目录 sys.path.append('./') #把读取模块的目录设置为当前目录 import tcstory #导入我的模块 NUMBER=-4 user_list=[] class People: count=0 def __init__(self,g_name='None',g_telephone='None',g_email='None'): self.name=g_name self.telephone=g_telephone self.email=g_email People.count+=1 def menu(): print("\t\t\t 欢迎来到通讯录") print("\t\t\t n:下一页 b:上一页") print('\t\t\tc:修改 s:保存 q:退出') def display(choice): global NUMBER,user_list os.system("clear") flag=1 if choice=='n': menu() NUMBER+=4 for i in range(4): try: print('\t\t\t'+str(NUMBER+i)+':'+user_list[NUMBER+i].name,user_list[NUMBER+i].telephone,user_list[NUMBER+i].email) flag=0 except IndexError: if flag: NUMBER-=4 flag=1 return elif choice=='b': menu() if People.count<=4:return NUMBER-=4 if NUMBER<0: NUMBER=-4 return for i in range(4): print('\t\t\t'+str(NUMBER+i)+':'+user_list[NUMBER+i].name,user_list[NUMBER+i].telephone,user_list[NUMBER+i].email) elif choice=='q': exit() elif choice=='c': os.system('clear') modify() elif choice=='s': with open('info.txt','w+') as data: for each in user_list: data.write(each.name+' ') data.write(each.telephone+' ') data.write(each.email+'\n') #由于write方法不会自动添加回车键,所以我自己添加 exit()
else: print("\t\t\t未知的输入") def load_info(): global user_list try: with open('info.txt','r+') as data: for f in data: temp=People() temp.name,temp.telephone,temp.email=f.split(' ') temp.email=temp.email.replace('\n','') #因为从文件读取的每一行的最后都有一个回车键,所以这里把他去掉: user_list.append(temp) except IOError as ioerr: print("File error:"+str(ioerr)) exit() def modify(): os.system('clear') global user_list print("\t\t\t1.新增 2.修改 3.删除") print("\t\t\tq:离开") choice=input("\t\t\t请输入您的选择: ") if choice=='1': tcstory.add(user_list) elif choice=='2': tcstory.change(user_list) elif choice=='3': tcstory.delete(user_list) else: os.system('clear')
def main(): global user_list choice='n' load_info() while True: display(choice) choice=input('\n\t\t\t请输入你的选择') if __name__=='__main__':main()
|