2015-04-27 20 views
0

我想通過使用字典進行多選題測驗。我擁有一切,但它重複了之前被問過的同樣的問題。我應該使用popitempop方法,還是別的?在顯示字典後刪除字符串

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() 
+1

使用循環和停止每次 –

+0

你的函數是遞歸調用該函數,所以即使你刪除一個元素從字典中,當函數被調用時,您再次添加它。 – logic

回答

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! 
1
# save the key you choose 
k = random.choice(keys); 

# store correct answer 
correctanswer = a_q[k] 

# remove question from dict 
del correctanswer[k] 

編輯:但你需要分配功能

+0

但正確答案[k]是一個字符串對象,我無法刪除字符串對象。感謝您的幫助 –

+0

函數被反覆調用,所以字典總是被重新創建,所以這將無法正常工作。 –

+0

@PadraicCunningham:對!已更新 – fferri

0

不要讓調用函數使用外循環字典,每次調用字典的重建等等功能這是毫無意義的嘗試刪除任何東西:

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! 
+0

感謝您的幫助,但是如果我錯過了一個問題,它仍會重複之前詢問過的問題嗎?我應該怎麼做? –

+0

使用inner while循環,考慮到只有三個可能的答案,您要再次嘗試多少次? –

+0

只有一次,實際上我的團隊和我正在使用pygame進行遊戲,但按鈕不工作,所以我試圖在shell中運行它。 –