2016-02-23 47 views
0

我正在嘗試做出多選(4個選擇)的問答遊戲。到目前爲止,我已經做了一個簡單的測驗,只包含一個問題。我無法圍繞索引問題的好方法。進行測驗,如何存儲問題?

該計劃是擴大我的測驗至少500個問題,並從問題池中隨機挑選一個問題。我應該如何構造它?

這是我在我的一個個問題的遊戲這麼遠:

def welcome(): #Introduction 
    print "Welcome to the quiz!" 
    print " " 
    print " " 


def question(): # Question function 
    quest = { 'id' : 0, 'question' : "What is the capital of Belgium?" , 'a' : "Vienna" , 'b' : "Berlin" , 'c' : "Brussels" , 'd' : "Prague" , 'answer' : 'c'} 
    print quest['question'] 
    print " " 
    print "A:", quest['a'], 
    print "B:", quest['b'], 
    print "C:", quest['c'], 
    print "D:", quest['d'] 

    guess=raw_input("Your guess: ") 
    if guess == quest['answer']: 
     print " " 
     print "Correct!!!" 
    else: 
     print " " 
     print "Sorry, that was just plain wrong!" 



welcome() 
question() 
+1

你的詞典是一個良好的開端!現在製作一個這樣的詞典列表。然後你可以使用'random.choice'來選擇一個隨機問題。 – L3viathan

回答

3

您可以創建一個列表的,字典,將容納所有這些數據。所以,你可以做這樣的事情:

quiz_data = [ 
    { 
     "question": "What year is it", 
     "choices": {"a": "2009", "b": "2016", "c": "2010"}, 
     "answer": "b" 
    }, 
    { 
     "question": "Another Question", 
     "choices": {"a": "choice 1", "b": "choice 2", "c": "choice 3"}, 
     "answer": "a" 
    } 
] 

然後使用random.choice來選擇你的數據結構的隨機指數。

import random 

q = random.choice(quiz_data) 

print(q.get('question')) 
answer = input(q.get('choices')).lower() 

if answer == q.get('answer'): 
    print("You got it") 
else: 
    print("Wrong") 
0

存儲它作爲一個JSON陣列

[{ 
    "id": 0, 
    "question": "What is the capital of Belgium?", 
    "a": "Vienna", 
    "b": "Berlin", 
    "c": "Brussels", 
    "d": "Prague", 
    "answer": "c" 
}] 

並加載它使用json.load

+0

我不確定* this *如何回答問題。 :/ – gsamaras

+0

@gsamaras它回答了「如何存儲的問題。」閱讀'json'的文檔應該足以實現一個能夠完成問題要求的解決方案。 –

+0

是的,我知道Tichodroma,但如果你給初學者足夠的信息,我不知道,但這是我的意見。 – gsamaras

0

您應該創建一個txt文件並將問題放入該文件中。之後,您可以使用random.choice()方法隨機讀取該文件的行並隨機選取一行(行是這裏的問題)。

基本上,你會把你的問題寫在一個txt文件中。然後閱讀行並用random.choice()打印一行(問題)。

創建另一個txt文件以查找答案,請在用戶回答問題時檢查該文件。

0

我會(根據你的代碼):

  1. 使用的問題清單。該清單將成爲問題的集合。
  2. 我也會放棄你的id屬性,我現在看不到 的原因。
  3. 我會在0到 這個列表的長度範圍內選一個隨機數-1,這樣我就可以索引這個問題池問一個問題用戶。
  4. 最後,我會接受用戶的答案,將其轉換爲 小寫,然後檢查答案是否正確。

下面是代碼:

#!/usr/bin/python 

from random import randint 

def welcome(): #Introduction 
    print "Welcome to the quiz!" 
    print " " 
    print " " 


def question(): # Question function 
    question_pool = [] 
    question_pool.append({'question' : "What is the capital of Belgium?" , 'a' : "Vienna" , 'b' : "Berlin" , 'c' : "Brussels" , 'd' : "Prague" , 'answer' : 'c'}) 
    question_pool.append({'question' : "Does Stackoverflow help?" , 'a' : "Yes" , 'b' : "A lot" , 'c' : "Of course" , 'd' : "Hell yeah" , 'answer' : 'd'}) 
    random_idx = randint(0, len(question_pool) - 1) 
    print question_pool[random_idx]['question'] 
    print " " 
    print "A:", question_pool[random_idx]['a'], 
    print "B:", question_pool[random_idx]['b'], 
    print "C:", question_pool[random_idx]['c'], 
    print "D:", question_pool[random_idx]['d'] 

    guess=raw_input("Your guess: ") 
    guess = guess.lower() 
    if guess == question_pool[random_idx]['answer']: 
     print " " 
     print "Correct!!!" 
    else: 
     print " " 
     print "Sorry, that was just plain wrong!" 



welcome() 
question() 

爲你下一步驟中,將驗證輸入,例如,以檢查用戶鍵入一個字母,A,B,C或D.

問題,幫助:

  1. Generate random integers between 0 and 9
  2. How to convert string to lowercase in Python?

我敢肯定柏林不是比利時:)

+0

謝謝,這對我很有幫助! (我非常確定答案是C:布魯塞爾;-) –

+0

哈哈OK @Newless_Cluebie,也許我沒有通過測驗:/很高興我幫助過,我也在這裏看到其他答案,確保你接受最好的答案! :) – gsamaras

+0

雖然可能有更好的答案,你幫我弄我一直在尋找的框架。作爲一個新手,最好的選擇並不總是你想要的,但至少讓我繼續前進。我稍後會回顧其他問題。保重,再次 - 非常感謝! –

0

資金我會嘗試導入隨機函數,然後生成1到(問題編號)之間的隨機數。假設你有10個問題,你可以像這樣輸入;

import random 
(Number) = (random.randint(1,10)) 

然後,添加所有的問題一個接一個,每個如圖下if語句;

if (Number) == (1): 
    print ("What's 1 + 1?") 
    (Answer) = input() 
    if (Answer) == ('2'): 
     print ('Correct!') 
    else: 
     print ('Wrong!') 

elif (Number) == (2): 
    print ("What's 1 + 2?") 
    (Answer) = input() 
    if (Answer) == ('4'): 
     print ('Correct!') 
    else: 
     print ('Wrong!') 

等等。

如果你想讓它重複,並要求多的問題,請與編碼;

while (1) == (1): 

然後你有一個工作測驗計劃。我希望有人發現這有幫助。

0

我認爲測驗作出過於複雜。 這段代碼更容易閱讀,並且更短。

point = 0 
    print("The answer have to be in all small letters") 
    def question(question,rightAnswer,rightAnswer2): 
     global point 
     answer = input(question) 
     if answer == (rightAnswer): 
      point = point + 1 
      print("Right") 
     elif answer == (rightAnswer2): 
      point = point + 1 
      print("Right") 
     else: 
      print("Wrong") 
     if point == 1: 
      print("You have",point,"point")   
     else:         # grammar 
      print("You have",point,"points")  
     return 

    question("What is 1+1? ","2","2") #question(<"question">,answer,otheranswer) 
    question("What is the meaning of life ","42","idk")