分数应记录在与学生班级有关的.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个最近的分数存储,或者我可以把它添加到我的第二个。
发布于 2015-02-14 12:46:40
这不是一个愚蠢的问题,所以我投了赞成票。但是,如果您想要创建一个很好的应用程序,那么您应该真正学习如何在python中处理类和使用对象。贝娄--我向您展示了一些关于如何将问题抽象为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...然后,在空闲的时候,你可能会有这样的东西:
>>> 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等等.
https://stackoverflow.com/questions/28515426
复制相似问题