Python 实现单词表录入和翻译功能
创建单词文件
word_file = open('words.txt', 'w') word_file.write('a: 一个\n') word_file.write('apple: 苹果\n') word_file.write('book: 书\n') word_file.write('cat: 猫\n') word_file.write('dog: 狗\n') word_file.close()
读取单词文件
word_dict = {} with open('words.txt', 'r') as word_file: for line in word_file: line = line.strip() if line: word, translation = line.split(': ') word_dict[word] = translation
翻译单词
word = input('请输入要翻译的单词:') if word in word_dict: print(word_dict[word]) else: print('No such word')
原文地址: https://www.cveoy.top/t/topic/mFIv 著作权归作者所有。请勿转载和采集!