Python 自限重函数

Python 自限重函数,python,function,limit,repeat,Python,Function,Limit,Repeat,我正在为a&p课程的当前部分编写一个基本上是学习指南/实践测试的程序(它让我比一遍又一遍地重读笔记更投入)。测试没有任何问题,但我有一个问题,我的一些问题使用了“enterbox”输入,如果答案不正确,我可以让问题循环,但如果没有正确的答案,我无法让它中断。 我找到了一种方法,将整个函数放回初始的“else”树中,这样无论是对是错,都可以进入下一个问题,但它看起来非常丑陋,我不相信没有更好的方法。 所以我的“解决方案”是这样的: def question82(): x = "which t

我正在为a&p课程的当前部分编写一个基本上是学习指南/实践测试的程序(它让我比一遍又一遍地重读笔记更投入)。测试没有任何问题,但我有一个问题,我的一些问题使用了“enterbox”输入,如果答案不正确,我可以让问题循环,但如果没有正确的答案,我无法让它中断。 我找到了一种方法,将整个函数放回初始的“else”树中,这样无论是对是错,都可以进入下一个问题,但它看起来非常丑陋,我不相信没有更好的方法。 所以我的“解决方案”是这样的:

def question82():
   x = "which type of metabolism provides the maximum amount of ATP needed for contraction?"
   ques82 = enterbox(msg = x, title = version)
   #version is a variable defined earlier
   if ques82.lower() in ["aerobic"]:
        add() #a function that is explained in the example further below
        question83()
   else:
        loss() #again its a housecleaning function shown below
        ques82b = enterbox(msg = x, title = version)
        if ques82b.lower() in ["aerobic"]:
            add()
            question83()
        else:
            loss()
            question83()
好吧,这样做是可行的,但对每个“enterbox”问题使用嵌套的if树看起来有点草率。我是自学成才的,所以这可能是唯一的解决办法,但如果有更好的办法,我很想了解它

下面是我的课程的完整部分:

from easygui import * 
import sys

version = 'A&P EXAM 3 REVIEW'
points = 0

def add():
    global points
    msgbox("Correct", title = version)
    points = points + 1

def loss():
    global points
    msgbox("Try Again", title = version)
    points = points - 1  

def question81():
    x = "What chemical is stored by muscle as a source of readily available energy for muscle contractions"
    ques81 = enterbox(msg = x, title = version)
    if ques81.lower() in ["creatine"]:
        add()
        question82()
    else:
        loss()
        question81()  
它的工作原理是这样的,所以所提供的任何错误都可能是我复制和粘贴的错误。 如果有帮助的话,我还在Python2.7rc1中运行它。 提前谢谢你的帮助


我不知道是否有一种方法可以组合带有“跳过”按钮的“输入框”,因为这也是一种解决方案。

考虑以下方法:

  • 我们定义了一个问答对列表。我们在一个地方执行此操作,因此易于维护,不必搜索整个文件来进行更改,也不必为不同的问题集重复使用此代码
  • 我们创建了一个
    ask\u question
    函数,可以调用该函数来处理所有问题。这样,如果我们想改变如何实现我们的问题逻辑,我们只需要在一个位置进行更改(而不是在每个
    questionXX
    函数中)
  • 我们使用
    ==
    中的
    来比较用户输入和答案(
    中的
    会做其他事情,而不是你所期望的)
  • 我们创建一个对象来跟踪答案结果。在这里,它是
    ResultsStore
    的一个实例,但它实际上可以是任何东西,让我们试着避开全局变量
  • 提示回答时使用循环。如果给出的答案不正确(如果
    重试失败为False),则循环将重复
  • 允许用户输入一些“跳过”关键字以跳过问题
  • “测试”完成后显示结果。在这里,我们通过定义和调用
    store.display\u results()
    方法来实现这一点
那么,关于:

from easygui import enterbox

question_answer_pairs = [
    ("1 + 1 = ?", "2"),
    ("2 * 3 = ?", "6"),
    ("which type of metabolism provides the maximum amount of ATP needed for contraction?", "aerobic")
]

VERSION = 'A&P EXAM 3 REVIEW'

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()
现在,返回值目前没有任何作用,但它可以

RES_MAP = {
    True: "Correct!",
    None: "(skipped)",
    False: "Incorrect"          # Will only be shown if retry_on_fail is False
}

for (q,a) in question_answer_pairs:
    res = ask_question(q, a, store)
    print(RES_MAP[res])

考虑以下方法:

  • 我们定义了一个问答对列表。我们在一个地方执行此操作,因此易于维护,不必搜索整个文件来进行更改,也不必为不同的问题集重复使用此代码
  • 我们创建了一个
    ask\u question
    函数,可以调用该函数来处理所有问题。这样,如果我们想改变如何实现我们的问题逻辑,我们只需要在一个位置进行更改(而不是在每个
    questionXX
    函数中)
  • 我们使用
    ==
    中的
    来比较用户输入和答案(
    中的
    会做其他事情,而不是你所期望的)
  • 我们创建一个对象来跟踪答案结果。在这里,它是
    ResultsStore
    的一个实例,但它实际上可以是任何东西,让我们试着避开全局变量
  • 提示回答时使用循环。如果给出的答案不正确(如果
    重试失败为False),则循环将重复
  • 允许用户输入一些“跳过”关键字以跳过问题
  • “测试”完成后显示结果。在这里,我们通过定义和调用
    store.display\u results()
    方法来实现这一点
那么,关于:

from easygui import enterbox

question_answer_pairs = [
    ("1 + 1 = ?", "2"),
    ("2 * 3 = ?", "6"),
    ("which type of metabolism provides the maximum amount of ATP needed for contraction?", "aerobic")
]

VERSION = 'A&P EXAM 3 REVIEW'

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()
现在,返回值目前没有任何作用,但它可以

RES_MAP = {
    True: "Correct!",
    None: "(skipped)",
    False: "Incorrect"          # Will only be shown if retry_on_fail is False
}

for (q,a) in question_answer_pairs:
    res = ask_question(q, a, store)
    print(RES_MAP[res])

快速而肮脏的解决方案可以使用默认值“跳过”作为答案:

def question81():
x = "What chemical is stored by muscle as a source of readily available energy for muscle contractions"
ques81 = enterbox(msg = x, title = version, default = "skip")
if ques81.lower() == 'creatine':
    add()
    question82()
elif ques81 == 'skip':
    # Do something
else:
    loss()
    question81() 
但是你应该认真研究犹太矮人给出的答案。有很多东西需要学习
程序设计。他不是给你鱼,他是教你钓鱼。

快速而肮脏的解决方案可以使用默认值“跳过”作为答案:

def question81():
x = "What chemical is stored by muscle as a source of readily available energy for muscle contractions"
ques81 = enterbox(msg = x, title = version, default = "skip")
if ques81.lower() == 'creatine':
    add()
    question82()
elif ques81 == 'skip':
    # Do something
else:
    loss()
    question81() 
但是你应该认真研究犹太矮人给出的答案。有很多东西需要学习
程序设计。他不是在给你鱼,他是在教你钓鱼。

除非你在某处定义了
lower()
函数,
如果是问题82,那么[“有氧”]中的lower():
应该会引发一个
语法错误。如果[“Obagood”]中的ques82.lower():
带有点而不是逗号,则应为
。这真的是您试图运行的确切代码吗?不,对不起,确切的代码是ques82.lower(),它运行时没有问题。我现在就编辑这个问题来纠正这个问题好的,我做了一些编辑。因为我从来没有投资过LTT的Synergy,我用我的笔记本电脑在这里和桌面上发帖,所以我无法复制和粘贴,所以我只是快速地键入了它。除非你在某处定义了
lower()
函数,
if ques82,lower()in[“需氧”]:
应该会引发
SyntaxError
。如果[“Obagood”]中的ques82.lower():
带有点而不是逗号,则应为
。这真的是您试图运行的确切代码吗?不,对不起,确切的代码是ques82.lower(),它运行时没有问题。我现在就编辑这个问题来纠正这个问题好的,我做了一些编辑。因为我从来没有投资过LTT的Synergy,我用我的笔记本电脑在这里和桌面上发帖,所以我无法复制和粘贴,所以我只是快速地把它打印出来。你不需要一个单独的人来存储结果,你可以将它设为一个普通的类,并将它传递给
提问
,或者让
提问
返回
-1,0,1
中的一个。错误、跳过、更正并将结果收集到
for
循环中…@bruno