class WordBook: def add_word(self): file = open('1.txt','a+') word = input('请输入要添加的单词:') file.write(word + '\n') file.close() print('单词添加成功') def remove_word(self): file = open('1.txt','r') word_list = file.readlines() print(word_list) word = input('请输入要删除的单词') word_list.remove(word + '\n') file.close() file2 = open('1.txt','w') file2.writelines(word_list) file2.close() print('删除成功!') def recite_word(self): file = open('1.txt','r') word_list = file.readlines() file.close() for i in word_list: print(i) flag = input('是否记住本单词(yes or no)') if flag == 'yes': continue else: while True : print(i) flag2 = input('是否记住本单词(yes or no)') if flag2 == 'yes': break print('恭喜你记住了所有的单词!') def clear_word_book(self): file = open('1.txt','w') file.write('') file.close() print('单词本已清空!') def menu(self): print('1.添加单词') print('2.删除单词') print('3.背诵单词') print('4.清空单词本') print('5.退出单词本') wordBook = WordBook() while True: wordBook.menu() id = input('请输入功能编号:') if id == '1': wordBook.add_word() elif id == '2': wordBook.remove_word() elif id == '3': wordBook.recite_word() elif id == '4': wordBook.clear_word_book() elif id == '5': print('感谢使用!') break else: print('共轭能编号输入错误!')
还有不完善的地方,望谅解。