• 需求分析
    在使用电脑上网冲浪,打游戏,看电影时,经常会在各个地方夹杂着一些看不懂的英文单词,为了在日常玩电脑的时候,也能利用碎片时间积累英文单词,因此自制了一款简易的背单词程序。功能如下:
    1.平时见到的单词,利用CTRL+C,CTRL+V保存在文本文件words.csv中。
    2.每次打开程序的时候,随机抽取100个单词来进行翻译答题测验考试,对于每一个单词,翻译测验正确的,跳过;翻译测验错误的,增加该单词在下次考试继续出现的概率。
  • 运行环境
    操作系统:Windows7
    程序语言及版本:python3.7
  • 文件名称及作用
    words.csv 用于保存英文单词和中文翻译,如performance,表演
    accumulation1.0.py 主程序文件的文件名
  • 源码
import random
import csv

def load_word_file():
	word_list = []
	with open('words.csv', 'r') as csvfile:
		content = csv.reader(csvfile)
		for i in content:
			word_list.append(tuple(i))
	return word_list


def examination():
	word_list = load_word_file()
	examination_quantity = 100
	if len(word_list) < examination_quantity:
		examination_quantity = len(word_list)
	for english_word, chinese_word in random.sample(word_list, examination_quantity):
		print('\n')
		print(chinese_word)
		answer = input('Please translate the word, your answer is?   ')
		if answer == english_word:
			print('Its correct.')
		else:
			append_word(english_word, chinese_word)
			print('wrong, the correct answer is', english_word)

def append_word(english_word, chinese_word):
	with open('words.csv', 'a', newline = '') as csvfile:
		writer = csv.writer(csvfile)
		writer.writerow([english_word, chinese_word])


if __name__ == '__main__':
	examination()
  • 使用方法:
    平时看到的单词按如下格式复制到words.csv中(如果用excel表打开就不用加逗号),有空的时候执行accumulation1.0.py 进行单词测验,对于测验错误的单词,下次测验再考的几率会增加。
    performance,表演
    apple,苹果
    orange,橙子
Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐