0
class Question(object):
def __init__(self, question, options, answer, description, points):
    self.question = question
    self.options = options
    self.answer = answer
    self.description = description
    self.points = points

def ask():
    response = None
    while response not in ("1", "2", "3", "4"):
        response = raw_input(self.question).lower()

    if response == self.answer:
        print "right"

    else:
        print "wrong"

问题=?答案=?

我希望它通过我的问题数量,但我想从文本文件中添加问题和其他值。

这就是我到目前为止所拥有的,但我真的很困惑如何从同一个文本文件中添加问题、答案、类别和点值..

文本文件示例 trivia.txt:

Category
Question
Choice 1
Choice 2
Choice 3
Choice 4
Answer (being a digit 1-4)
Description why it is that answer
Point Value
4

1 回答 1

0

分号分隔即

Category:Question:Choice 1:Choice 2:Choice 3:Choice 4:etc

将它们读入并将它们传递给您的对象:

with open('questions.txt', 'r') as testfile:
    for line in testfile:
        question = (line.split(':'))

您知道有一个可迭代列表,可用于为问题分配值。这只是一种可能的解决方案。

您还可以查看字典路由:

从字符串创建字典

于 2013-11-01T19:11:17.760 回答