首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在用户定义的字典中有多个正确答案

在用户定义的字典中有多个正确答案
EN

Stack Overflow用户
提问于 2018-07-24 17:39:38
回答 1查看 319关注 0票数 0

我在为一堂药理学课写复习测验。我根据收到的不同问题的反馈切换了风格,但遇到了问题。我希望有多个正确的答案,因为用户可能会输入略有不同的答案。我之前使用的格式应该是这样的:

代码语言:javascript
复制
   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()

"Skip“和"Loss”只是跟踪跳过和错误输入的基本功能。

现在我尝试使用的新格式是:

代码语言:javascript
复制
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()

(上面的代码几乎是从另一个回答不同问题的用户那里逐字摘取的,它可以工作,但我不认为它是写出来的)好的,希望复制和粘贴是正确的,但它在第一个问题上运行良好,但在第二个问题上崩溃,返回:

代码语言:javascript
复制
  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

我试着去掉括号,在答案之间加上"or“,但得到了同样的错误。在这一点上,我几乎是在摸索,因为我甚至不完全确定如何寻找帮助。

我完全是自学的,所以所有Barney级别的解释对我来说都很重要。谢谢

编辑:所以把逗号放在第6行纠正了这个问题。但是我现在得到了以下错误

代码语言:javascript
复制
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'

但是,如果我删除带有多个答案的问题,它就会消失。

EN

回答 1

Stack Overflow用户

发布于 2018-07-24 17:46:50

您缺少一个逗号,所以您实际上有一个类似于[(...), (a, b)(c, d)]的列表,而不是像[(...), (a, b), (c, d)]这样的列表,在这个列表中,它认为您使用参数(c, d)调用第二个对象(a, b)

变化

代码语言:javascript
复制
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")
]

代码语言:javascript
复制
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")
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51495352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档