我正在嘗試創建測驗。在文本文件中,我有由主題,問題,答案和空白空間組成的塊(按該順序)。每條線代表這些項目之一:從文本文件中獲取四行文本塊
組織學巨核細胞起源於什麼?血小板。
生理學在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行塊分組然後對它們進行隨機化?如果我可以按主題和問題過濾它們會更好。