2016-03-06 48 views
0

我必须创建一个测验,询问用户的姓名以及他们所在的课程,之后我需要将结果格式化为各自的.csv文件。我已经创建了测验,并将结果写入三个独立的.csv文件(每个类有一个),但我不知道如何按字母顺序格式化结果。需要帮助字母化测验结果.CSV

import random #random functions 
import os #provides a way of using operating system dependent functions 
import csv 

print ("Welcome! What is your name?") #asks name and starts quiz 
name = input().title() #capitalises the name for future printing 
Class = str(input ("What class are you in? ")) 
while Class != "1" and Class != "2" and Class != "3": 
    Class=str(input ("What class are you in? ")) 

print ("Welcome to the quiz" ,name,"!") 
score = 0 #sets score to 0 
Q = 0 #sets question number to 0 
while Q < 10: #only asks questions up to ten of them being asked 
    operator = random.randint (1,3) 
number1 = random.randint (1,10) 
number2 = random.randint (1,10) 
if operator == 1: #addition q's 
    print ("What is", number1, "+", number2,"?") 
    ans = number1 + number2 

elif operator == 2: #subtraction q's 
    print ("What is", number1, "-", number2,"?") 
    ans = number1 - number2 

else: #does the only other option, multplication q's 
    print ("What is", number1, "*", number2,"?") 
    ans = number1 * number2 

while True: 
    try: 
     InputAns = int(input()) #the answer the user input 
    except ValueError: #Stops anything but integers being used 
     print ("Please enter a number") 
     continue 
    else: 
     break #stops a loop 

if InputAns == ans: #input answer equals true answer 
      print ("Thats correct!") 
      score += 1 #adds a plus one to their score 

else: #When the input answer doesnt equal the true answer 
      print ("Sorry, thats incorrect") 



Q += 1 #Keeps asking question until it gets to 10, this part adds one to the Q number each time. 

if score<5 and score>0: 
print ("Sorry", name, "you only got",score, "out of 10") 

elif score==0: 
print ("Commiserations",name, 

     "you got",score, "out of 10!") 

else: 
    print ("Congratulations", name, "you got", score, "out of 10") #shows user their score 


if Class == "1": 
    with open('Class 1 Results.csv', 'a') as f: 
     file_writer = csv.writer(f, delimiter=',',lineterminator='\n') 
     file_writer.writerow((name, score)) #stores any user in Class 1's  results in their respective table 

elif Class == "2":  
    with open('Class 2 Results.csv', 'a') as f: 
     file_writer = csv.writer(f, delimiter=',',lineterminator='\n') 
     file_writer.writerow((name, score)) #stores any user in Class 2's results in their respective table 

elif Class == "3":  
    with open('Class 3 Results.csv', 'a') as f: 
     file_writer = csv.writer(f, delimiter=',',lineterminator='\n') 
     file_writer.writerow((name, score)) #stores any user in Class 3's results in their respective table 

回答

0

您可以在写入文件后对数据进行排序。对于排序,将读取数据,然后使用Operator模块进行排序,然后重新编写排序后的数据。

import random #random functions 
import os #provides a way of using operating system dependent functions 
import csv 
import operator 

def sortcsv(csvfilename, columnNumberforSort): 
    data = csv.reader(open(csvfilename),delimiter=',') 
    sortedlist = sorted(data, key=operator.itemgetter(columnNumberforSort)) 
    with open(csvfilename, "w") as f: 
     fileWriter = csv.writer(f, delimiter=',',lineterminator='\n') 
     for row in sortedlist: 
      fileWriter.writerow(row) 

print ("Welcome! What is your name?") #asks name and starts quiz 
name = input().title() #capitalises the name for future printing 
Class = str(input ("What class are you in? ")) 
while Class != "1" and Class != "2" and Class != "3": 
    Class=str(input ("What class are you in? ")) 

print ("Welcome to the quiz" ,name,"!") 

score = random.randint(1,100) 

if Class == "1": 
    with open('Class 1 Results.csv', 'a') as f: 
     file_writer = csv.writer(f, delimiter=',',lineterminator='\n') 
     file_writer.writerow((name, score)) #stores any user in Class 1's  results in their respective table 
    sortcsv('Class 1 Results.csv', 0) 

elif Class == "2":  
    with open('Class 2 Results.csv', 'a') as f: 
     file_writer = csv.writer(f, delimiter=',',lineterminator='\n') 
     file_writer.writerow((name, score)) #stores any user in Class 2's results in their respective table 
    sortcsv('Class 2 Results.csv', 0) 

elif Class == "3": 
    with open('Class 3 Results.csv', 'a') as f: 
     file_writer = csv.writer(f, delimiter=',',lineterminator='\n') 
     file_writer.writerow((name, score)) #stores any user in Class 3's results in their respective table 
    sortcsv('Class 3 Results.csv', 0) 
+0

谢谢!我将如何按平均分数进行组织并从最高到最低? – SamH314

+0

完成从最低到最高,然而Average对我造成麻烦,我还需要记录每个学生的最后三个分数,之后是否有任何方法可以删除? – SamH314