按字母顺序排列和数字文本文档的排序不起作用

时间:2015-01-06 14:16:38

标签: python sorting alphabetical

我一直致力于个人项目,我遇到了一个完全停止了我的开发的问题。这段代码应该能够:

  • 向用户打印一组10个基本问题,这些问题是随机生成的,并且使用加法,减法和乘法。它应该接受答案,然后告诉用户他们的答案是否正确。然后它会更新他们的分数。
  • 该代码将存储每个人的分数,以及他们的名字和分数。
  • 存储的信息必须能够按字母顺序,数字顺序和平均值进行排序。

我已经完成了前2项任务。 (如下面的代码所示。随意运行它!)

我需要一些排序方面的帮助。这是我的错误:

  

ValueError:基数为10的int()的文字无效:''

以下是所有代码(没有排序功能):

    def quiz () :
    for i in range(0,1):
        import random
        from operator import add, sub, mul;

        # Asks for a name.
        name = input("What is your name? ")
        # Asks for the group number.
        group = input("Which class are you in? 1, 2 or 3? ")

        operations = (add, sub, mul)
        operator = random.choice(operations)

        # Sets the users score to zero.
        score = 0

        # Sets the indented part of code to repeat 10 times. 10 Questions will be printed.
        for i in range(0,10):

            calculations = ('+', '-', '*')
            operator = random.choice(calculations)

            if operator == "+":
                calculation = add
            if operator == "-":
                calculation = sub
            if operator == "*":
                calculation = mul

            #  This function adds the first and second number into the sum. It also determines what numbers will be displayed, between 1 and 10.      
            a, b, d, c = (random.randint(1,10), operator, random.randint(1,10),calculation)

        #Prints the question
            print(a, b, d)

        #Creates an input for the answer
            acd = int(input("Your answer: "))

            e = calculation(a, d)

        #Marks the question, and udates the users score depending on result of answer. Correct or Incorrect.
            if acd == e:
                print('Correct')
                score = score+1

            elif acd:
                print('Incorrect')
                score = score+0
# Gives the user brief feedback abou their score.
        if score == 10:
            print('Well done, you are great at maths ' +str(name))
        if score <6:
            print('You should try harder next time!' +str(name))
        if score <3:
            print('You should take extra maths lessons!' +str(name))

        print("Your score is: " +str(score))
# Outputs the data to a text document, sorted by class.
        if group == "1":
            with open("class1results.txt", "a") as myfile:
                myfile.write("\nName: " +str(name) +"\n  Score: " +str(score) )
                myfile.close()
                print('Your test data has been recorded.')
# Outputs the data to a text document, for group 2.               
        if group == "2":
            with open("class2results.txt", "a") as myfile:
                myfile.write("\nName: " +str(name) +"\n  Score: " +str(score) )
                myfile.close()
                print('Your test data has been recorded.')
# Outputs the data to a text document, for group 3.
        if group == "3":
            with open("class3results.txt", "a") as myfile:
                myfile.write("\nName: " +str(name) +"\n  Score: " +str(score) )
                myfile.close()
                print('Your test data has been recorded.')        

        alphabetSort()

# Asks the user to take the quiz again.
        question = input("Would you like to run the quiz for another student?")
        if question == "Yes":
                quiz ()
        if question == "yes":
                quiz()
        if question == "No":
            exit()
        if question == "no":
            exit()



# Runs the quiz again, to start the looping off.
quiz()

我知道这很复杂,但确实有效! (差不多!:/)

1 个答案:

答案 0 :(得分:0)

首先,您应该将您的分数存储为JSON或其他一些易于解析的格式

import re
def get_scores(fname):
    scores = {}
    for name,score in re.findall("Name: (.*)\n Score: (.*)",open(fname).read()):
        if name in scores:
           scores[name].append(score)
        else:
           scores[name] = [score]
    return scores

这将为您提供映射到分数列表的名称字典

 {"bob":[1,2,3],"Sam":[5,6,7],...}

现在你编写了排序函数

 def sort_by_name(data):
     return sorted(data)
 def sort_by_max_score(data):
     return sorted(data,key=lambda x:max(data[x]))
 def sort_by_average(data):
     return sorted(data,key=lambda x:float(sum(data[x]))/len(data[x])

然后你只需用解析器创建的字典调用你的sort函数