我想通過使用字典進行多選題測驗。我擁有一切,但它重複了之前被問過的同樣的問題。我應該使用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()
使用循環和停止每次 –
你的函數是遞歸調用該函數,所以即使你刪除一個元素從字典中,當函數被調用時,您再次添加它。 – logic