有没有办法在 Python 词典中附加第三个值?
Is there a way to attach a 3rd value in Python dictionaries?
这可能是一个愚蠢的问题,我相信我很有可能采用了完全错误的方法来解决我的问题,所以如果有人知道更好的方法,请随时纠正我/指出我正确的方向。
我正在尝试在 Python 中制作一个基本的测验程序来获得乐趣,但我似乎找不到一种好方法来实际调用问题及其答案并存储正确答案应该是什么是。理想情况下,我也想在问题中打乱答案,例如,#1 的答案并不总是 "A",但目前这一步太过分了。
无论如何,我觉得字典可以用。我会这样做:
test = {
'1': "What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #A
'2': "What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", #B
'3': "What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", #C
'4': "What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #D
'5': "What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", #C
'6': "What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", #B
}
answer = raw_input(test['1'])
我可以使用它轻松地按顺序或随机地打印出问题,稍作修改。但是,我无法将正确答案实际附加到字典中,这意味着我必须做一些事情,比如制作大量的 if
语句。有谁知道解决方案?有没有办法为每个字典条目添加第三个值?我完全忽略的解决方法?任何帮助将不胜感激。
将字典的值变成元组:
'1': ("What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", 'A')
然后您可以通过 test['1'][0]
访问问题,通过 test['1'][1]
访问答案。
将值放入 list
中,如
test = {
'1': ["What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#A' ]
'2': ["What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", '#B' ]
'3': ["What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", '#C' ]
'4': ["What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#D' ]
'5': ["What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", '#C' ]
'6': ["What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", '#B' ]
}
然后访问
question = raw_input(test['1'][0])
answer = raw_input(test['1'][1])
您的数据结构布局似乎有误。您应该使用字典列表,而不是其他答案所建议的元组或列表字典。
questions = [
{'question':"What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",
'answer':"#A"},
{'question':"What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n",
'answer':"#B"},
...}
这将让您通过以下方式遍历您的问题:
for q in questions:
print(q['question'])
ans = input(">> ")
if ans == q['answer']:
# correct!
else:
# wrong!
如果你仍然需要数字,你可以将 number
保存为字典的另一个键(使这种类似于数据库中的行, number
成为主键):
{'number': 1,
'question': ...,
'answer': ...}
或者只是使用 enumerate
和 start
关键字参数
来遍历您的问题
for q_num, q in enumerate(questions, start=1):
print("{}. {}".format(q_num, q['question']))
...
放弃字典,让 python 为您完成更多工作。您可以用一个列表表示每个 question/answer 组,其中第一项是问题,最后一项是答案,中间的所有项目都是多项选择答案。将它们放在另一个列表中,很容易将它们洗牌。
import string
import random
# a list of question/choices/answer lists
test = [
# question choices.... answer
["What is 1+1?", "2", "11", "1", "None of the above", 0],
["What is 2+2?", "2", "11", "4", "None of the above", 2],
# ... more questions ...
]
# shuffle the test so you get a different order every time
random.shuffle(test)
# an example for giving the test...
# 'enumerate' gives you a count (starting from 1 in this case) and
# each test list in turn
for index, item in enumerate(test, 1):
# the question is the first item in the list
question = item[0]
# possible answers are everything between the first and last items
answers = item[1:-1]
# the index to the real answer is the final item
answer = item[-1]
# print out the question and possible answers. 'string.uppercase' is a list
# of "ABC...XYZ" so we can change the index of the answer to a letter
print "%d) %s" % (index, question)
for a_index, answer in enumerate(answers):
print " %s) %s" % (string.uppercase[a_index], answer)
# prompt for an answer
data = raw_input("Answer? ")
# see if it matches expected
if data == string.uppercase[answer]:
print "Correct!"
else:
print "No... better luck next time"
这可能是一个愚蠢的问题,我相信我很有可能采用了完全错误的方法来解决我的问题,所以如果有人知道更好的方法,请随时纠正我/指出我正确的方向。
我正在尝试在 Python 中制作一个基本的测验程序来获得乐趣,但我似乎找不到一种好方法来实际调用问题及其答案并存储正确答案应该是什么是。理想情况下,我也想在问题中打乱答案,例如,#1 的答案并不总是 "A",但目前这一步太过分了。
无论如何,我觉得字典可以用。我会这样做:
test = {
'1': "What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #A
'2': "What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", #B
'3': "What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", #C
'4': "What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", #D
'5': "What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", #C
'6': "What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", #B
}
answer = raw_input(test['1'])
我可以使用它轻松地按顺序或随机地打印出问题,稍作修改。但是,我无法将正确答案实际附加到字典中,这意味着我必须做一些事情,比如制作大量的 if
语句。有谁知道解决方案?有没有办法为每个字典条目添加第三个值?我完全忽略的解决方法?任何帮助将不胜感激。
将字典的值变成元组:
'1': ("What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", 'A')
然后您可以通过 test['1'][0]
访问问题,通过 test['1'][1]
访问答案。
将值放入 list
中,如
test = {
'1': ["What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#A' ]
'2': ["What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n", '#B' ]
'3': ["What is 3+3?\nA) 2\nB) 11\nC) 6\nD) None of the above.\n\n", '#C' ]
'4': ["What is 4+4?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n", '#D' ]
'5': ["What is 5+5?\nA) 2\nB) 11\nC) 10\nD) None of the above.\n\n", '#C' ]
'6': ["What is 6+6?\nA) 2\nB) 12\nC) 1\nD) None of the above.\n\n", '#B' ]
}
然后访问
question = raw_input(test['1'][0])
answer = raw_input(test['1'][1])
您的数据结构布局似乎有误。您应该使用字典列表,而不是其他答案所建议的元组或列表字典。
questions = [
{'question':"What is 1+1?\nA) 2\nB) 11\nC) 1\nD) None of the above.\n\n",
'answer':"#A"},
{'question':"What is 2+2?\nA) 2\nB) 4\nC) 1\nD) None of the above.\n\n",
'answer':"#B"},
...}
这将让您通过以下方式遍历您的问题:
for q in questions:
print(q['question'])
ans = input(">> ")
if ans == q['answer']:
# correct!
else:
# wrong!
如果你仍然需要数字,你可以将 number
保存为字典的另一个键(使这种类似于数据库中的行, number
成为主键):
{'number': 1,
'question': ...,
'answer': ...}
或者只是使用 enumerate
和 start
关键字参数
for q_num, q in enumerate(questions, start=1):
print("{}. {}".format(q_num, q['question']))
...
放弃字典,让 python 为您完成更多工作。您可以用一个列表表示每个 question/answer 组,其中第一项是问题,最后一项是答案,中间的所有项目都是多项选择答案。将它们放在另一个列表中,很容易将它们洗牌。
import string
import random
# a list of question/choices/answer lists
test = [
# question choices.... answer
["What is 1+1?", "2", "11", "1", "None of the above", 0],
["What is 2+2?", "2", "11", "4", "None of the above", 2],
# ... more questions ...
]
# shuffle the test so you get a different order every time
random.shuffle(test)
# an example for giving the test...
# 'enumerate' gives you a count (starting from 1 in this case) and
# each test list in turn
for index, item in enumerate(test, 1):
# the question is the first item in the list
question = item[0]
# possible answers are everything between the first and last items
answers = item[1:-1]
# the index to the real answer is the final item
answer = item[-1]
# print out the question and possible answers. 'string.uppercase' is a list
# of "ABC...XYZ" so we can change the index of the answer to a letter
print "%d) %s" % (index, question)
for a_index, answer in enumerate(answers):
print " %s) %s" % (string.uppercase[a_index], answer)
# prompt for an answer
data = raw_input("Answer? ")
# see if it matches expected
if data == string.uppercase[answer]:
print "Correct!"
else:
print "No... better luck next time"