我正在编写一个代码,您可以在这里提出数学问题。我已将学生的分数与他们的名字一起保存在3个不同的班级的不同文件夹中。然后我为他们完成的每项测试都有三个不同的文件。我想要一个代码来检查他们的名字是否在每个文件中。然后,当它检查每个文件中是否有名称时,我想从人员结果中读取所有三个分数。然后按平均值对三个分数进行排序并打印出来,但对于每个完成测试三次的人来说。
到目前为止,这是我的代码,询问问题,然后将名称和分数保存到文件等中:
import random
Name =input("What is your name?")
Class =input("What class are you in? (1/2/3)")
TimesDone = input("How many times have you done the test before(1-3)")
Score = 1
Questions = 1
while Questions < 10:
Randomnumber1 = (random.randint(1,10))
Randomnumber2 = (random.randint(1,10))
Operators = ['+','-','*']
Operators1 = random.choice(Operators)
print(Randomnumber1, Operators1, Randomnumber2)
if Operators1 == '+':
answer = (Randomnumber1 + Randomnumber2)
elif Operators1 == '-':
answer = (Randomnumber1 - Randomnumber2)
elif Operators1 == '*':
answer = (Randomnumber1 - Randomnumber2)
UserGuess = int(input("What is the answer to the question?"))
if UserGuess == answer:
print("Well done",Name,"you got the correct answer!")
Questions = Questions+1
Score = Score+1
else:
print("Wrong answer",Name,"the correct answer is",answer,".")
Questions = Questions+1
if Questions == 10:
print("You answered all 10 questions and your final score",Score,".")
if Class == '1':
if TimesDone == '1':
myfile = open('namesforclass1test1.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass1test1.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif TimesDone == '2':
myfile = open('namesforclass1test2.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass1test2.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif TimesDone == '3':
myfile = open('namesforclass1test3.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass1test3.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif Class == '2':
if TimesDone == '1':
myfile = open('namesforclass2test1.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass2test1.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif TimesDone == '2':
myfile = open('namesforclass2test2.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass2test2.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif TimesDone == '3':
myfile = open('namesforclass2test3.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass2test3.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif Class == '3':
if TimesDone == '1':
myfile = open('namesforclass3test1.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass3test1.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif TimesDone == '2':
myfile = open('namesforclass3test2.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass3test2.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
elif TimesDone == '3':
myfile = open('namesforclass3test3.txt','a')
names = (Name)
myfile.write(repr(names))
myfile.write('\n')
myfile.close()
myfile = open('scoresforclass3test3.txt','a')
scores = (Score)
myfile.write(repr(scores))
myfile.write('\n')
myfile.close()
答案 0 :(得分:0)
编辑:在了解用户尝试做什么之后更新了答案。
我对此代码的第一个问题是它非常冗余。 Python遵循DRY(不要重复自己)的方法。获取冗余代码并从中创建一个函数,并使用不同的参数多次调用该函数。例如:
def write_data_to_file(filename, data):
myfile = open(filename,'a')
myfile.write(repr(data))
myfile.write('\n')
myfile.close()
接下来,我将对您用于解决问题的数据结构进行更改。使用列表字典可以帮助我们。例如:
# Instead/ Before storing the data in to text files, store them in dictonaries.
student_score_dict_timesdone_1.update({name: score))
student_score_dict_timesdone_2.update({name: score))
student_score_dict_timesdone_3.update({name: score))
def merged_scores(student_score_dict_timesdone_1,student_score_dict_timesdone_2,student_score_dict_timesdone_3):
keys_list=[]
keys_list.append(student_score_dict_timesdone_1.keys())
keys_list.append(student_score_dict_timesdone_2.keys())
keys_list.append(student_score_dict_timesdone_3.keys())
keys_list=list(set(keys_list))
master_dict = {}
for key in key_list():
scores_list=[]
if key in student_score_dict_timesdone_1:
scores_list.append(student_score_dict_timesdone_1[key])
if key in student_score_dict_timesdone_2:
scores_list.append(student_score_dict_timesdone_2[key])
if key in student_score_dict_timesdone_3:
scores_list.append(student_score_dict_timesdone_3[key])
master_dict.update({name:scores_list})
return master_dict
def average_scores(master_dict):
average_score_dict={}
for key in master_dict:
key_average_score=sum(master[key])/len(master[key]) # This is integer division. Hence only floor values would be saved.
average_score_dict.update({key:key_average_score})
return average_score_dict