这可能是一个愚蠢的问题,我相信我很有可能采取完全错误的方法来解决我的问题,所以如果有人知道更好的方法,请随意纠正我/指出我在右边方向。
我正在尝试用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
语句的事情。有谁知道解决方案?有没有办法为每个字典条目添加第3个值?一个解决方案,我完全忽略了?任何帮助将不胜感激。
答案 0 :(得分:5)
将字典的值设为元组:
'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]
访问答案。
答案 1 :(得分:5)
您似乎错误地布置了数据结构。您应该使用字典列表,而不是像其他答案所暗示的元组或列表字典。
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']))
...
答案 2 :(得分:3)
将值放入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])
答案 3 :(得分:1)
抛弃字典,让python为你做更多的工作。您可以使用列表表示每个问题/答案组,其中第一项是问题,最后一项是答案,其间的所有项都是多项选择答案。把它们放在另一个列表中,很容易将它们改组。
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"