我正在尝试使用字典进行多项选择测验。我有一切,但它重复了之前被问过的同样的问题。我应该使用popitem
,pop
方法还是其他方法?
def German():
import random
a_q = {'Berlin':'What is east capital city for German?',
'Bonn':'What is west capital city for German?',
'Michael Schumacher':'What is a famous formula one driver?',
'Albert Einstein':'He was one of the smartest one.'}
keys = [x for x in random.sample(a_q, 3)]
correctanswer = a_q[random.choice(keys)]
correctanswer.popitems()
print 'Question: ', correctanswer
key1, key2, key3 = keys[0], keys[1], keys[2]
print '\nA. %s \nB. %s \nC. %s' % (key1, key2, key3)
A, B, C = a_q[key1], a_q[key2], a_q[key3]
answer = raw_input('What is the right answer? ')
if answer == "A":
if A == correctanswer:
print "That's correct!"
German()
else:
print "I'm sorry, that is incorrect"
print('')
German()
elif answer == "B":
if B == correctanswer:
print "That's correct!"
German()
else:
print "I'm sorry, that is incorrect"
German()
elif answer == "C":
if C == correctanswer:
print "That's correct!"
German()
else:
print "I'm sorry, that is incorrect"
German()
else:
print "That is not a valid selection."
German()
答案 0 :(得分:1)
gt
编辑:但您需要在功能
之外指定字典答案 1 :(得分:0)
不要一直调用函数使用循环,每次调用函数时都会重新创建dict,因此尝试删除任何东西都是毫无意义的:
def German():
import random
a_q = {'Berlin': 'What is east capital city for German?',
'Bonn': 'What is west capital city for German?',
'Michael Schumacher': 'What is a famous formula one driver?',
'Albert Einstein': 'He was one of the smartest one.'}
samp = random.sample(a_q.items(), 3)
qs = [v for _, v in samp]
ans = [k for k, _ in samp]
A, B, C = ans
d = {"A": A,"B": B,"C": C}
for a, q in zip(ans, qs):
print 'Question: {}'.format(q)
print '\nA. {} \nB. {} \nC. {}' .format(A, B, C)
answer = raw_input('What is the right answer? ')
# check answer against a
if d[answer] == a:
print("That is correct")
else:
print("Sorry that is incorrect")
输出:
Question: What is a famous formula one driver?
A. Michael Schumacher
B. Berlin
C. Albert Einstein
What is the right answer? A
That's correct!
Question: What is east capital city for German?
A. Michael Schumacher
B. Berlin
C. Albert Einstein
What is the right answer? B
That's correct!
Question: He was one of the smartest one.
A. Michael Schumacher
B. Berlin
C. Albert Einstein
What is the right answer? C
That's correct!
答案 2 :(得分:0)
正如我在comments of your question中提到的,你的函数是递归的,所以即使你从字典中删除了一个元素,你也可以在调用函数时重新添加它。
要解决此问题,您可以使用以下代码行:
q = [x for x in random.sample(a_q, len(a_q))]
这会创建一个随机排序的问题列表,因此您可以在没有任何重复问题的情况下进行迭代。 无论您添加多少问题,这都会有效。
现在,对于答案,你可以使用这段代码:
c=random.sample(a_q, 3)
while c.count(q[i]) != 1:
c=random.sample(a_q, 3)
random.shuffle(c)
这将创建一个包含3个答案的随机列表,其正确答案显示为q[i]
。 while循环用于防止答案池中的任何重复答案。
如果你把它放在一起:
def German():
import random
a_q = {'Berlin':'What is east capital city for German?',
'Bonn':'What is west capital city for German?',
'Michael Schumacher':'What is a famous formula one driver?',
'Albert Einstein':'He was one of the smartest one.',
'The Berlin Wall':'What was the wall called that separated the country?',
'Boris Becker':'He was a good tennis player he won two wimbildom titles?',
'2006':'What year was the FIFA World Cup held in Germany?',
'3':'How many world cups does Germany have?',
'Yodeling':'What is one of the tradition in German?',
'9 months':'How long does Zugspitze winter last for?'}
q = [x for x in random.sample(a_q, len(a_q))] #create list of questions
i=0 #start counter
while i < len(q): #iterate through every question
correctanswer = a_q[q[i]] #define the correct answer
print 'Question: ', correctanswer
c=random.sample(a_q, 3)
while c.count(q[i]) != 1:
c=random.sample(a_q, 3) #get the answer pool
key1, key2, key3 = c
print '\nA. %s \nB. %s \nC. %s' % (key1, key2, key3)
A, B, C = a_q[key1], a_q[key2], a_q[key3]
answer = raw_input('What is the right answer? ')
while answer not in 'ABCD': #check for invalid inputs
print "That is not a valid selection.\n"
answer = raw_input('What is the right answer? ')
if answer == 'A' and A == correctanswer:
print "That's correct!"
elif answer == 'B' and B == correctanswer:
print "That's correct!"
elif answer == 'C' and C == correctanswer:
print "That's correct!"
else:
print "I'm sorry, that is incorrect"
i+=1
German()
这将输出:
>>>
Question: How many world cups does Germany have?
A. Bonn
B. Yodeling
C. 3
What is the right answer? 3
That is not a valid selection.
What is the right answer? C
That's correct!
Question: What is one of the tradition in German?
A. Yodeling
B. Boris Becker
C. The Berlin Wall
What is the right answer? A
That's correct!
Question: What year was the FIFA World Cup held in Germany?
A. Bonn
B. Yodeling
C. 2006
What is the right answer? C
That's correct!
Question: How long does Zugspitze winter last for?
A. Albert Einstein
B. Bonn
C. 9 months
What is the right answer? C
That's correct!
Question: What is east capital city for German?
A. Berlin
B. 9 months
C. Boris Becker
What is the right answer? A
That's correct!
Question: He was one of the smartest one.
A. Albert Einstein
B. The Berlin Wall
C. 2006
What is the right answer? A
That's correct!
Question: What is west capital city for German?
A. Bonn
B. Yodeling
C. 9 months
What is the right answer? B
I'm sorry, that is incorrect
Question: What is a famous formula one driver?
A. Michael Schumacher
B. The Berlin Wall
C. Yodeling
What is the right answer? A
That's correct!
Question: He was a good tennis player he won two wimbildom titles?
A. Bonn
B. Berlin
C. Boris Becker
What is the right answer? C
That's correct!
Question: What was the wall called that separated the country?
A. Boris Becker
B. The Berlin Wall
C. Michael Schumacher
What is the right answer? B
That's correct!