Как я уже упоминал в 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!
Используйте цикл и остановить вызов функции каждый раз, когда –
Ваша функция является рекурсивной, так что даже если вы удалите элемент из словаря, вы добавляете его снова, когда функция вызывается. – logic