用户定义的词典中有多个正确答案

时间:2018-07-24 09:39:38

标签: python python-2.7 dictionary

我正在写有关药理课的复习测验。我根据收到的其他问题的反馈来切换样式,但是遇到了问题。我希望有多个正确答案,因为用户输入的答案可能略有不同。 我以前使用的格式看起来像这样:

   x = "What is the starting dose for oral Enalipril in HTN?"
   ques1 = enterbox(msg = x, title = version)
   if ques1.lower() in ["2.5-5", "2.5-5mg","2.5mg"]:
        add() 
        #which is a function I had described above to track points and display that the answer was correct
        ques2()
   elif ques1.lower() in ["skip"]:
        skip()
        que2()
   else:
        loss()
        que1()

“跳过”和“丢失”只是用于跟踪跳过和错误输入的基本功能。

现在我要使用的新格式是:

from easygui import enterbox

question_answer_pairs = [
    ("What Class of medication is Enalapril?", "ACE Inhibitor"),
    ("What is the starting oral dose of Enalapril for HTN?", ["2.5-5mg", "2.5-5","2.5mg"]),
    ("which type of metabolism provides the maximum amount of ATP needed for contraction?", "aerobic")
]

VERSION = 'Pharmacology Prep Exam'

class ResultStore:
    def __init__(self):
        self.num_correct = 0
        self.num_skipped = 0
        self.num_wrong = 0

    def show_results(self):
        print("Results:")
        print("  Correct:", self.num_correct)
        print("  Skipped:", self.num_skipped)
        print("  Wrong:  ", self.num_wrong)


def ask_question(q, a, rs, retry_on_fail=True):
    while True:
        resp = enterbox(msg=q, title=VERSION)
        # Force resp to be a string if nothing is entered (so .lower() doesn't throw)
        if resp is None: resp = ''
        if resp.lower() == a.lower():
            rs.num_correct += 1
            return True
        if resp.lower() == "skip":
            rs.num_skipped += 1
            return None

          # If we get here, we haven't returned (so the answer was neither correct nor
        #   "skip").  Increment num_wrong and check whether we should repeat.
        rs.num_wrong += 1
        if retry_on_fail is False:
            return False

# Create a ResultsStore object to keep track of how we did
store = ResultStore()

# Ask questions
for (q,a) in question_answer_pairs:
    ask_question(q, a, store)

# Display results (calling the .show_results() method on the ResultsStore object)
store.show_results()

(上面的代码几乎是逐字逐句地从回答了一个不同问题的其他用户那里获取的,虽然可以,但是我不认为要写出它)  好的,希望复制和粘贴正确,但是它对第一个问题运行正常,但在第二个问题上崩溃,返回:

  File "./APPrac.py", line 7, in <module>
    ("which type of metabolism provides the maximum amount of ATP needed for contraction?", "aerobic")
TypeError: 'tuple' object is not callable

我尝试摆脱括号并在答案之间插入“或”,但出现相同的错误。 此时,我几乎在黑暗中射击,因为我什至不完全确定如何寻求帮助。

我完全是自学成才的,因此所有Barney级别的解释对我来说意味着世界。 谢谢

编辑:因此将逗号放在第6行中可以解决此问题。但是我现在收到以下错误

Traceback (most recent call last):
   File "./dict.py", line 47, in <module>
     ask_question(q, a, store)
  File "./dict.py", line 29, in ask_question
     if resp.lower() == a.lower():
AttributeError: 'list' object has no attribute 'lower'

如果我删除有多个答案的问题,它就消失了。

1 个答案:

答案 0 :(得分:1)

您缺少逗号,因此实际上没有一个看起来像[(...), (a, b), (c, d)]的列表,而是认为它用第二个对象[(...), (a, b)(c, d)]调用了第二个对象(a, b)参数(c, d)

更改

question_answer_pairs = [
    ("What Class of medication is Enalapril?", "ACE Inhibitor"),
    ("What is the starting oral dose of Enalapril for HTN?", ["2.5-5mg", "2.5-5","2.5mg"])
    ("which type of metabolism provides the maximum amount of ATP needed for contraction?", "aerobic")
]

question_answer_pairs = [
    ("What Class of medication is Enalapril?", "ACE Inhibitor"),
    ("What is the starting oral dose of Enalapril for HTN?", ["2.5-5mg", "2.5-5","2.5mg"]),  # <-- this comma was missing
    ("which type of metabolism provides the maximum amount of ATP needed for contraction?", "aerobic")
]