【发布时间】:2016-03-01 14:05:27
【问题描述】:
让我换个问题。我将如何从所需的班级中提取所有用户信息(分数、班级编号和分数)。例如,我想知道让班级编号 1 中的所有用户信息。
帮助,schooldata[x]['class_code'] = 用户输入的班级编号 schooldata[x]['score'] = 该学生获得的分数 schooldata[x]['name'] = 该用户的姓名 [x] 是学生,所以第一个用户是 [0],第二个用户是 [1] 等等......
schooldata = []
for x in range (0,3): #the number of loops the quiz is run which is 3 times
score = 0
quiz = dict()
print ("Enter your name")
quiz['name'] = input()
print ("what class")
quiz['class_code'] = input()
print("1. 9+10=")
answer = input()
answer = int(answer)
if answer == 19:
print("correct")
score = score + 1
else:
print("wrong")
print("2. 16+40=")
answer = input()
answer = int(answer)
if answer == 56:
print("correct")
score = score + 1
else:
print("wrong")
print("3. 5+21=")
answer = input()
answer = int(answer)
if answer == 26:
print("correct")
score = score + 1
else:
print("wrong")
print("4. 5-6=")
answer = input()
answer = int(answer)
if answer == -1:
print("correct")
score = score + 1
else:
print("wrong")
print("5. 21-9=")
answer = input()
answer = int(answer)
if answer == 12:
print("correct")
score = score + 1
else:
print("wrong")
print("6. 12-11=")
answer = input()
answer = int(answer)
if answer == 1:
print("correct")
score = score + 1
else:
print("wrong")
print("7. 5*6=")
answer = input()
answer = int(answer)
if answer == 30:
print("correct")
score = score + 1
else:
print("wrong")
print("8. 1*8=")
answer = input()
answer = int(answer)
if answer == 8:
print("correct")
score = score + 1
else:
print("wrong")
print("9. 4*6=")
answer = input()
answer = int(answer)
if answer == 24:
print("correct")
score = score + 1
else:
print("wrong")
print("10. 9*10=")
answer = input()
answer = int(answer)
if answer == 90:
print("correct")
score = score + 1
else:
print("wrong")
quiz['score'] = score
schooldata.append(quiz)
print ("name - ", schooldata[0]['name'],", user score - ", schooldata[0]['score'],", class number - ", schooldata[0]['class_code'])
print ("name - ", schooldata[1]['name'],", user score - ", schooldata[1]['score'],", class number - ", schooldata[1]['class_code'])
print ("name - ", schooldata[2]['name'],", user score - ", schooldata[2]['score'],", class number - ", schooldata[2]['class_code'])
#high to low
sorted_schooldata = sorted(schooldata, key=lambda k: k['score'])[::-1]
#alphabetical
for i in sorted(schooldata, key=lambda k: k['name']):
print('%s:%s' %(i['name'], i['score']))
【问题讨论】:
-
不清楚你在问什么,或者代码 sn-p 与你的“问题”有什么关系
-
那么..有什么问题?您的预期与实际输出是多少?你给你的程序什么输入?你有错误吗?不要要求我们为你做功课。我们可以帮助您解决困难,但不要要求我们完成整个任务。
-
你想要
schooldata[0]、schooldata[1]、schooldata[2]的平均分? -
例如,如果我是老师,我想知道一个班级所有分数的平均分。因此,如果 3 个用户输入他们在 1 号班级,那么第 2 个用户将是 2 号班级,并说第 3 个用户输入他们在 1 号班级。我作为老师想要找到所有用户的平均分数让我们说第 1 课。所以当老师输入是时,我的代码应该输出第 1 课中所有用户的姓名和该课的平均分数。希望这是有道理的
-
这是一段巨大的代码。您能否提及您用于存储值的数据结构示例以及您想要哪个值的平均值?