我正在为小学生创建一个测验

时间:2015-02-14 12:01:34

标签: python sorting

分数应记录在与学生班级相关的.txt文件中。然后老师应该能够选择一个班级。然后,教师可以选择是否按字母顺序查看结果,从最新的三个结果或最高到最低的分数中查看每个学生的平均分数。

我已经完成了课程部分但是我在为教师选择课程和选择排序结果的方法编写代码时遇到了麻烦。

到目前为止,这是我的代码:

import random


score=0
name= input('What is your name?')

def class_():
    classNum=input('What class are you in 1,2 or 3?')
    global inputfile
    if classNum=="1": inputfile=open("class1.txt","a")
    elif classNum=="2": inputfile=open("class2.txt","a")
    else: inputfile=open("class3.txt", "a")


print ('Welcome to my quiz', str(name))
print ('You will be asked 10 random questions to test your maths skills')
input("Press Enter to continue...")

range(10)
operatorList=["+","-","*"]
numQuestions=10



for q in range (numQuestions):
    op=random.choice(operatorList)
    first_num = random.randint(0,10)
    second_num = random.randint(0,10)
    print(first_num,op,second_num)
    expression = "%d %s %d" % (first_num, op, second_num) 
    answer = eval(expression) 
    reply = int(input('Enter the answer: '))

    if (reply==answer):
        print("Well done, you got it right!")
        score=score+1
    else:
         print("Unlucky! You got it wrong")


print ('\n')
print ('\n')


print("Well done", str(name))
print("You scored", str(score)+"/10")

class_()
inputfile.write(name + "," + str(score)+ "\n")
inputfile.close()

我认为你必须为老师创建一个全新的代码

新代码

好的,这是我新代码的开头:

viewclass= input("choose a class number and either alphabetically, average or highest?")


if viewclass=='1 alphabetically':
    with open('class1.txt', 'r') as r:
        for line in sorted(r):
             print(line, end='')

elif viewclass=='2 alphabetically':
    with open('class2.txt', 'r') as r:
        for line in sorted(r):
             print(line, end='')

elif viewclass=='3 alphabetically':
    with open('class3.txt', 'r') as r:
        for line in sorted(r):
             print(line, end='')

所以我已经完成了按字母顺序排列的部分,但我必须更改第一个代码,只允许为每个学生存储3个最新分数,或者我可以将其添加到第二个分数。

1 个答案:

答案 0 :(得分:1)

这不是一个愚蠢的问题,所以我对它进行了投票。但是你应该学习如何处理类并在python中使用对象,如果你想要做一个很好的应用程序。 Bellow我向您展示了如何将问题抽象为python类的一些技巧,它并不好,我不太清楚你需要什么,但这只是一个开始。

class Class(object):
    def __init__(self, num, name, input_file):
        self.name = name
        self.num = num
        self.input_file = input_file
        self.students = []
        #so far it's just an empty list

    #add methods that read in the students from a file
    #parse it for info you need
    def parse():
        #parse self.input_file for info you need
        pass

class Classes(object):
    def __init__(self, input_file):
        self.input_file = input_file
        self.all_classes = []
    #this parse function should read in from a file
    #of all classes, and instantiate a new Class
    #object and put them in self.all_classes
    #this is the class that enables you to select
    #which Class you want, and Class object should
    #have methods for calculating average, per student
    #grade etc....
    def parse():
        #parse the file to fill self.all_classes
        pass


classes = Classes(input_file="all_classes.txt")
#this should return a Classes object named classes
#that object should contain a list of all Class objects
#those objects should have methods to retrieve avg grade etc...

然后在IDLE你可能会有这样的东西:

>>> classes.all_classes
[]
>>> classes.input_file
'all_classes.txt'
>>> specific_class = classes.all_classes["specific_class_identifier"]
>>> specific_class["specific_quiz_identifier"].avgGrade()

我建议您在实际编程之前计划您想要实现的目标。特别是您保存信息的数据文件。即它们可以像csv分隔文件一样简单: 的 all_classes.txt

class1a,"class1a.txt"

<强> specific_class.txt

"Student Student","quiz1: grade","quiz2: grade"
"Student Student","quiz1: grade","quiz2: grade"

或者可能是像JSON等复杂文件......