jeudi 30 avril 2015

If I have three different text file how do I read them then sort them by average?

Hi I am currently writing a code where you ask maths questions. I have saved the students score along with their name in different folders for 3 different classes. Then I have three different files for each test they have done. I want a code that checks if their name is in each file. Then when it checks if the name is in each file I would like to read all three scores from the persons results. Then sort the three scores by average and print it but for each person who has done the test three times.

Here is my code so far that asks the questions and then saves the names and scores to a file etc. :

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()

Thanks, I would be very grateful if anyone could help me as I have tried many different ways and they didn't work. Sorry that the indents are off a bit but I couldn't manage to fix it

Aucun commentaire:

Enregistrer un commentaire