2013-10-14 31 views
0

我正在嘗試創建測驗。在文本文件中,我有由主題,問題,答案和空白空間組成的塊(按該順序)。每條線代表這些項目之一:從文本文件中獲取四行文本塊

組織學巨核細胞起源於什麼?血小板。

生理學在Glanzmann's中沒有發生哪種生理過程 血小板無力症?血小板聚集。

組織學在紅細胞生成過程中,細胞是否會失去其核?在色層階段時。

生理學哪個階段的止血功能的作用是凝血因子的作用是 ?二次止血。

生理學關節積血的特徵是什麼?血在聯合空間。

生理學除了循環之外,一部分血小板也儲存在 。哪裏?脾。

生理學哪個血小板區域包括submembranous 區域?周邊區域。

我已經成功編寫了一個程序,向用戶顯示問題,然後在用戶這樣說的時候顯示答案。但是,我想隨機顯示問題。我之前用它來展示它的靈感來自Michael Dawson的「絕對初學者的Python編程」一書。我遵循作者密切關注的結構,它的工作原理。代碼是:

#File opening function. Receives a file name, a mode and returns the opened file. 
def open_file(file_name, mode): 
    try: 
     file = open(file_name, mode) 
    except: 
     print("An error has ocurred. Please make sure that the file is in the correct location.") 
     input("Press enter to exit.") 
     sys.exit() 
    else: 
     return file 

#Next line function. Receives a file and returns the read line. 
def next_line(file): 
    line = file.readline() 
    line = line.replace("/", "\n") 
    return line 

#Next block function. Receives a file and returns the next block (set of three lines comprising subject, question and answer. 
def next_block(file): 
    subject = next_line(file) 
    question = next_line(file) 
    answer = next_line(file) 
    empty = next_line(file) 
    return subject, question, answer, empty 

#Welcome function. Introduces the user into the quizz, explaining its mechanics. 
def welcome(): 
    print(""" 
     Welcome to PITAA (Pain In The Ass Asker)! 
    PITAA will ask you random questions. You can then tell it to 
    reveal the correct answer. It does not evaluate your choice, 
    so you must see how many you got right by yourself. 
    """) 

def main(): 
    welcome() 
    file = open_file("quizz.txt", "r") 
    store = open_file("store.bat", "w") 
    subject, question, answer, empty = next_block(file) 
    while subject: 
     print("\n") 
     print("Subject: ", subject) 
     print("Question: ", question) 
     input("Press enter to reveal answer") 
     print("Answer: ", answer) 
     print("\n") 
     subject, question, answer, empty = next_block(file) 
    file.close() 
    print("\nQuizz over! Have a nice day!") 

#Running the program 
main() 
input("Press the enter key to exit.") 

如何將4行塊分組然後對它們進行隨機化?如果我可以按主題和問題過濾它們會更好。

回答

1
import random 

def open_file(file_name, mode): 
    try: 
     file = open(file_name, mode) 
    except: 
     print("An error has ocurred. Please make sure that the file is in the correct location.") 
     input("Press enter to exit.") 
     sys.exit() 
    else: 
     return file 

def replace_linebreaks(value): 
    value.replace("/", "\n") 

def main(): 
    welcome() 
# store = open_file("store.bat", "w") 
    file = open_file("quizz.txt", "r") 
    questions = file.read().split('\n\n') # if UNIX line endings 
    file.close() 
    random.shuffle(questions) 

    for question in questions.splitlines(): 
     subject, question, answer, empty = map(replace_linebreaks, question) 

     print("\n") 
     print("Subject: ", subject) 
     print("Question: ", question) 
     input("Press enter to reveal answer") 
     print("Answer: ", answer) 
     print("\n") 
     subject, question, answer, empty = next_block(file) 
    print("\nQuizz over! Have a nice day!") 
1

組織我會做一個簡單的類或使用字典。例如:

類實現

class Quiz(): 

    def __init__(self, question, answer, subject): 
     self.question = question 
     self.answer = answer 
     self.subject = subject 

可以使這些問題的實例,併爲他們每個人的主題,根據自己的屬性訪問它們。因此:

q = Quiz("Question 1", "Answer 1", "Chemistry") 
print(q.subject) 
>>> Chemistry 

您可以將新實例添加到列表中,只是隨機的列表,這樣

import random #Look up the python docs for this as there are several methods to use 

new_list = [] 
new_list.append(q) 
random.choice(new_list) #returns a random object in the list 

您還可以使用嵌套的字典做到這一點,基於「主體」向下鑽取

new_dict = {'subject': {'question': 'this is the question', 
         'answer': 'This is the answer'}} 

但我覺得通過創建自己的類更容易組織。

希望有一點幫助...