用如Tkinter或wxPython等GUI开发库开发程序用户界面,通过同用户界面交互,程序至少完成以下基本功能:
1、添加英文单词:能对单词添加多重中文释义,不同释义用逗号分开, 单词保存在txt文本格式的词典文件中,英文单词放在行首,与中文释义空格隔开,每行一个英语单词及其中文释义。添加英语单词要考虑单词合法性和存在等情况。词典文件中最终不少于50个单词。
2、删除英文单词。
3、查询英文单词。
4、单词测试:每次测试输入用户名,选择测试时的状态,如:状态极好、状态良好等5档。由系统在词典中随机抽取20个英文单词进行测试,只要能答对一种中文释义,就算对。用文件记录该用户的答题时间和对错情况。每个小组成员作为测试用户,必须测试一次。
绘制本组测试用户的多级雷达图(一个图包括本组所有用户),至少包含状态、答题速度和答题准确率三个属性。
5、退出。
话不多说,上代码
import os
import random #用于实现单词测试的随机抽取单词功能
import time #计时
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
fo = open("dict.txt", "r",encoding='utf-8') #以读的形式打开词典文件
words = {} #初始化数组用于存储单词信息
count = 0 #记录收录的单词数量
for line in fo.readlines(): #通过for循环将文件中已有的单词信息存入数组
s = str(line).split()
words[s[0]] = s[1]
count += 1
fo.close()
while True:
#每次循环重新打印菜单界面,方便用户作出选择
option = eval(input("********************"
"\n1 添加"
"\n2 查询"
"\n3 删除"
"\n4 单词测试"
"\n5 单词测试分析图"
"\n0 退出"
"\n********************\n"))
if option == 1: #添加功能
english = input("请输入英文单词: ") #读取用户输入
if english in words: #判断并处理多重释义情况
print("该单词已经添加到词典里!")
option_1 = eval(input("为{}添加多重释义?,1.是 2.否。".format(english))) #防止重复释义的情况
if option_1 == 1:
chinese = input("请输入中文释义:")
words[english] = words[english] + "," + chinese #在原有释义后加上新的释义
elif option_1 == 2:
continue
else:
print("输入有误!") #防止错误输入
continue
else:
chinese = input("请输入中文释义: ")
words[english] = chinese
count += 1 #记录总单词数
elif option == 2: #查询功能
english = input("请输入你要查询的英文单词:")
if english not in words: #判断输入是否有误
print("字典库中未找到这个单词!")
continue
else:
print("{}的中文释义是:{}".format(english, words[english]))
elif option == 3: #删除功能
english = input("请输入英文单词: ")
if english in words: #防止错误输入
words=words.pop(english) #在数组中移除该单词
#将其余单词数据移动到新的文件并覆盖原有文件,达到在文件中删除该单词的效果
lines = (i for i in open('dict.txt', 'r') if english not in i)
f = open('dict_new.txt', 'w')
f.writelines(lines)
f.close()
os.rename('dict.txt', 'dict.bak')
os.rename('dict_new.txt', 'dict.txt')
os.remove('dict.bak')
print("删除完成")
else:
print("输入有误")
elif option == 4: #单词测试功能
user=input("请输入用户名:")
print("请选择你的状态:"
"\n1.状态极好"
"\n2.状态良好"
"\n3.昏迷"
"\n4.溺水"
"\n5.濒死")
temp=eval(input())
if temp == 1:status="状态极好"
elif temp == 2:status="状态良好"
elif temp == 3:status="昏迷"
elif temp == 4:status="溺水"
elif temp == 5:status="濒死"
old_time = time.time() #记录答题时间
i = 0 #记录题目数量
z = 0 #记录答题正确的次数
curr={} #记录答题对错情况
cord='对错情况:'
while i < 5:
key = random.choice(list(words)) #随机从词典中抽取单词
right_key = words[key] #正确答案
right_key.split(',') #解决多重释义情况
print(key)
user_key = input("请输入这个单词的释义:")
if user_key in right_key: #判断对错情况并记录
print('恭喜您,此题答对了')
z = z + 1
curr[i] ='对'
else:
print('很遗憾,此题打错了,请再接再厉')
print('正确答案是:{}'.format(right_key))
curr[i] ='错'
i = i + 1
print('恭喜您,本次模拟结束,本次您的正确率为:{:.2%}'.format(z / 5))
for i in range(5):
cord +=curr[i]+","
print(cord) #打印对错情况
current_time = time.time() #记录答题时间
print("答题时间为" + str(current_time - old_time) + "s")
# 将结果存入txt文件
fo = open("record.txt", "a")
r1="用户名:"+user+" 状态:"+status+" 答题时间:"+str(current_time - old_time)+"s "+"答题准确率:"+str(z / 5)
fo.write(r1+cord+'\n')
fo.close()
if user in {'用户A', '用户B', '用户C', '用户D'}:
fo=open("record_num.txt","a")
r2=str(temp)+" "+str((current_time - old_time)/5)+" "+str(z*10 / 5)
fo.write(r2+'\n')
fo.close()
continue
elif option == 5: #绘制本组测试用户的多级雷达图
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
radar_labels = np.array(['用户A', '用户B', '用户C', '用户D'])
nAttr = 4 # 图的边数
#建议优化这个功能
#从文件读取数据并绘图,问题点:1.文件的数据需要严格控制为4行,如果同一用户测了两次会报错
# 2.不能精确定位某一用户的数据,如果用户D先测,在图里会显示为用户A的数据
#建议:根据用户数量动态调整图的数据(有点难) or 让新的数据覆盖原有数据,如用户B测了多次,取最近一次的数据覆盖第二行(比前一个简单点)
fo = open("record_num.txt", "r")
data = []
for line in fo.readlines():
s = line.split()
s = np.array([s[0], s[1], s[2]])
s = s.astype(np.float)
data.append(s)
fo.close()
data_labels = ('状态', '答题速度', '答题准确率') # 属性标签
angles = np.linspace(0, 2 * np.pi, nAttr, endpoint=False)
data = np.concatenate((data, [data[0]])) # 数据
angles = np.concatenate((angles, [angles[0]]))
fig = plt.figure(facecolor="white")
plt.subplot(111, polar=True)
plt.plot(angles, data, 'bo-', color='gray', linewidth=1, alpha=0.2)
plt.plot(angles, data, 'o-', linewidth=1.5, alpha=0.2)
plt.fill(angles, data, alpha=0.25)
plt.thetagrids((angles * 180 / np.pi)[:-1], radar_labels)
plt.figtext(0.52, 0.95, '单词测试分析图', ha='center', size=20) # 标题
legend = plt.legend(data_labels, loc=(0.94, 0.80), labelspacing=0.1)
plt.setp(legend.get_texts(), fontsize='small')
plt.grid(True)
plt.savefig('holland_radar.jpg')
plt.show()
elif option == 0:
break
else:
print("输入有误")
break
#将新收录的单词记录到词典文件中
items = list(words.items())
fo = open("dict.txt", "w")
for i in range(count):
english, chinese = items[i]
s = english + " " + chinese + '\n'
fo.write(s)
fo.close()
tips:要先在python文件的同目录下建立dict.txt
有关图如下