我通过多个网站试图解决这个问题,但我不知所措。
我的程序需要打印学生姓名(使用输入找到),班级名称(使用输入找到),然后是最终的3个分数。我找不到通过任何网站将分数附加到字典的方法,这只会给我带来错误。
很抱歉,我们认为可能需要大量的代码片段。
import random
import operator
import time
end = False
scores = dict()
def randCalc():
operators = {"+":operator.add,
"-":operator.sub,
"*":operator.mul}
num1 = random.randint(1,10)
num2 = random.randint(1,10)
op = random.choice(list(operators.keys()))
answer = operators.get(op)(num1,num2)
print("What is {} {} {}?\n".format(num1, op, num2))
return answer
def askQuest():
answer = randCalc()
guess = float(input())
guess = float(guess)
return guess == answer
def quiz():
print("WELCOME TO THE MATH QUIZ\n")
global student
student = input("What is your name? ")
global classname
classname = input("Which class are you in? (1, 2, 3) ")
global score
score = 0
for i in range(10):
correct = askQuest()
if correct:
score += 1
print("Correct!\n")
else:
print("Incorrect!\n")
classSelect()
while end == False:
time.sleep(2)
quiz()
答案 0 :(得分:2)
Idk如果我理解你,但你可以尝试使用json(与dict()相同)并拥有:
quizScores={}
quizScores[score1]=name;#score1 is score of first quiz
quizScores[score2]=name;#score2 is score of first quiz
等等。
答案 1 :(得分:1)
我不明白你为什么要score
成为关键。每个密钥必须是唯一的,以分数为关键因此,只有在您想要存储每个分数的学生数组时才有意义。如果您的问题是每个键存储多个值,则可以存储数组:
d = {"a": [1,2,3], "b": [4,5,6]}
print d["b"] # will print [4, 5, 6]
如果由于某种原因需要稍后搜索值,可以通过dict中的键/值对执行线性搜索:
d = {"a": [1,2,3], "b": [4,5,6]}
search_array = [1,2,3]
for key, value in d.iteritems():
if value == search_array:
print key # will print a