我正在做一个小测验。在文本文件中,我有由主题、问题、答案和一个空空间(按这个顺序)组成的块。每一行代表以下项目之一:
巨核细胞起源于什么?血小板。 Glanzmann的血栓性虚证没有发生生理过程?血小板聚集。 在促红细胞生成过程中,细胞是否失去了细胞核?在嗜色阶段的时候。 凝血因子的作用主要表现在止血的哪个阶段?二次止血。 血液病的生理特征是什么?关节间隙的血迹。 生理上,除了在循环中,一部分血小板也被储存。哪里?脾脏。 哪些血小板区包括膜下区?周边区域。
我已经成功地编写了一个程序,该程序向用户显示问题,然后当用户这样说时显示答案。然而,我想随机显示这些问题。我用来顺序显示它们的东西是由Michael的“面向绝对初学者的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行,然后将它们随机化?如果我能通过主题和问题来过滤它们,那就更好了。
发布于 2013-10-14 15:57:30
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!")发布于 2013-10-14 16:16:56
为了组织起来,我会制作一个简单的类或使用dicts。例如:
类实现
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'}}但我觉得创建自己的班级更容易组织起来。
希望这能帮点忙..。
https://stackoverflow.com/questions/19364051
复制相似问题