我正在尝试创建一个测验。在一个文本文件中,我有由主题、问题、答案和一个空格(按该顺序)组成的块。每行代表其中一项:
组织学 巨核细胞起源于什么?血小板。
生理学 Glanzmann 的血小板无力症不会发生哪些生理过程?血小板聚集。
组织学 在红细胞生成过程中,细胞何时失去其细胞核?当处于嗜正染色阶段时。
生理学止血的哪个阶段具有凝血因子的作用?二次止血。
生理学 关节积血有什么特点?关节间隙有血。
生理学 除了在循环中,一部分血小板也被储存起来。在哪里?脾脏。
生理学 哪些血小板区域包括膜下区域?外围区。
我已经成功编写了一个程序,该程序向用户显示问题,然后在用户说出答案时显示答案。但是,我想随机显示问题。我用来按顺序显示它们的内容是受到 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 行的块分组,然后将它们随机化?如果我可以按主题和问题过滤它们会更好。