python实现录入单词表 准备5个单词及翻译存入单词文件例: a 一个 apple: 苹果翻译单词: 输入单词把翻译打印出来如果没有这个单词则报No such word
创建单词文件
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/beJA 著作权归作者所有。请勿转载和采集!