I am trying to sort a data file by student name so that I get all of the student names with their classcode and score on the same line. I manage to sort in name order but the data is not split on to separate lines nor does it put the associated classcode or score with each name.
The code is:
import random
q=0
ans=0
studentnames = []
ascending = []
classcode = input("Enter your class (1,2 or 3): ")
if classcode == '1':
name=input("Enter your name: ")
n= int(input("Enter the number of questions you want to attempt: "))
#writing details to a txt file group1
text_file1 = open("data.dat", "a")
while q<n:
c = random.randint(1,3)
print("c is: ", c)
if c==1:
f="+"
num1 = random.randint(0,12)
print("num1 is: ", num1)
num2 = random.randint(0,10)
print("num2 is: ", num2)
product=num1*num2
difference=num1-num2
total=num1+num2
d=total
q+=1
print("\n Question ", q)
print ("What is ",num1,f,num2," ? ")
l=int(input("Enter Your Answer: "))
if l==d:
print ("\nCongratulations! your answer is right.\n")
ans+=1
else:
print("\n I'm afraid your answer is wrong. The correct one is" ,d)
elif c==2:
f="*"
num1 = random.randint(0,12)
print("num1 is: ", num1)
num2 = random.randint(0,10)
print("num2 is: ", num2)
product=num1*num2
difference=num1-num2
total=num1+num2
d=product
q+=1
print("\n Question ", q)
print ("What is ",num1,f,num2," ? ")
l=int(input("Enter Your Answer: "))
if l==d:
print ("\nCongratulations! your answer is right.\n")
ans+=1
else:
print("\n I'm afraid your answer is wrong. The correct one is" ,d)
else:
f="-"
num1 = random.randint(0,12)
print("num1 is: ", num1)
num2 = random.randint(0,10)
print("num2 is: ", num2)
product=num1*num2
difference=num1-num2
total=num1+num2
d=difference
q+=1 # counter for questions
print("\n Question ", q)
print ("What is ",num1,f,num2," ? ")
l=int(input("Enter Your Answer: "))
if l==d:
print ("\nCongratulations! your answer is right.\n")
ans+=1 # counter for answers
else:
print("\n I'm afraid your answer is wrong. The correct one is" ,d)
# calculating the percentage
x=float(ans)
y=float(q)
z=ans*100/q
cp=int(z)
print("Your Correct percentage = ",cp,"%\n")
#store details in a list
scores=[classcode,name,cp]
print(scores)
#writing to text file
text_file1.write(classcode )
text_file1.write(' ')
text_file1.write(name )
text_file1.write(' ')
#converting score to string so that it can be written to txt file
scr = str(cp)
text_file1.write(scr )
#adding a new line after each students score
text_file1.write("\n")
#close the txt file after it has been used
text_file1.close()
#reading the txt file
text_file1 = open("data.dat", "r")
print(text_file1.read())
text_file1.close()
f = open("data.dat", "r")
for line in f:
fields = line.split()
classcode = fields[0]
name = fields[1]
scr = str(fields[2])
studentnames.append(classcode)
studentnames.append(name)
studentnames.append(scr)
print(classcode, name, scr)
f = open("data.dat", "r")
for line in f:
fields = line.split()
classcode = fields[0]
name = fields[1]
scr = str(fields[2])
studentnames.sort()
ascending.append(studentnames)
print(ascending)
The data file that I am trying to sort in name order is:
1 test2 100
1 test1 100
1 test3 20
1 test4 60
1 test5 33
1 aguero 100
1 aguero 100
1 aguero 100
1 aguero 50
1 test3 60
1 1 100
1 test3 50
1 test2 100
1 test1 100
1 test4 50
1 messi 100
1 messi 100
1 ronaldo 100
1 ronaldo 100
1 maradonna 100
1 maradonna 100
1 maradonna 100
Thanks