自限性重复功能
Self Limiting Repition Function
我正在编写一个程序,基本上是我 A&P 当前部分的学习指南/练习测试 class(它让我比一遍又一遍地重读笔记更投入)。测试没有任何问题,但我有一个问题,我的一些问题使用 "enterbox" 输入,如果答案不正确,我可以让问题循环,但如果没有正确的答案,我就无法让它中断回答。
我找到了一种方法,通过将整个函数放回初始的 "else" 树来使其工作,这样无论对错,你都可以进入下一个问题,但它看起来非常丑陋,我不敢相信有'没有更好的方法。
所以我的 "solution" 看起来像这样:
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()
它按原样工作,所以所提供内容的任何错误都可能是我复制和粘贴的错误。
如果有帮助的话,我也在 python 2.7rc1 中 运行 它。
提前感谢您的帮助。
我不知道是否有一种方法可以将 "enterbox" 与 "skip" 的按钮结合起来,因为这也是一个解决方案。
考虑以下方法:
- 我们定义了一个问答对列表。我们在一个地方执行此操作,因此它易于维护,而且我们不必搜索整个文件来进行更改或将此代码重新用于不同的问题集。
- 我们创建了一个
ask_question
函数,我们可以为 所有 问题调用该函数。这样,如果我们想改变我们如何实现我们的问题逻辑,我们只需要在一个地方进行(而不是在每个 questionXX
函数中)。
- 我们使用
==
而不是 in
将用户输入与我们的答案进行比较(in
会做其他事情,而不是您期望的)。
- 我们创建一个对象来跟踪我们的回答结果。在这里,它是
ResultsStore
的一个实例,但它实际上可以是任何东西,让我们尝试摆脱全局变量。
- 提示回答时使用循环。如果给出的答案不正确(并且如果
retry_on_fail is False
),循环将重复。
- 允许用户输入一些 "skip" 关键字来跳过问题。
- "test" 完成后显示结果。在这里,我们通过定义和调用
store.display_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()
现在,return 值目前没有任何作用,但它可以!
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])
快速而肮脏的解决方案可以使用默认值 "skip" 作为答案:
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()
但是jedwards给出的答案你真的要研究一下。有很多东西要学
程序设计。他不是授人以鱼,而是授人以渔。
我正在编写一个程序,基本上是我 A&P 当前部分的学习指南/练习测试 class(它让我比一遍又一遍地重读笔记更投入)。测试没有任何问题,但我有一个问题,我的一些问题使用 "enterbox" 输入,如果答案不正确,我可以让问题循环,但如果没有正确的答案,我就无法让它中断回答。 我找到了一种方法,通过将整个函数放回初始的 "else" 树来使其工作,这样无论对错,你都可以进入下一个问题,但它看起来非常丑陋,我不敢相信有'没有更好的方法。 所以我的 "solution" 看起来像这样:
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()
它按原样工作,所以所提供内容的任何错误都可能是我复制和粘贴的错误。 如果有帮助的话,我也在 python 2.7rc1 中 运行 它。 提前感谢您的帮助。
我不知道是否有一种方法可以将 "enterbox" 与 "skip" 的按钮结合起来,因为这也是一个解决方案。
考虑以下方法:
- 我们定义了一个问答对列表。我们在一个地方执行此操作,因此它易于维护,而且我们不必搜索整个文件来进行更改或将此代码重新用于不同的问题集。
- 我们创建了一个
ask_question
函数,我们可以为 所有 问题调用该函数。这样,如果我们想改变我们如何实现我们的问题逻辑,我们只需要在一个地方进行(而不是在每个questionXX
函数中)。 - 我们使用
==
而不是in
将用户输入与我们的答案进行比较(in
会做其他事情,而不是您期望的)。 - 我们创建一个对象来跟踪我们的回答结果。在这里,它是
ResultsStore
的一个实例,但它实际上可以是任何东西,让我们尝试摆脱全局变量。 - 提示回答时使用循环。如果给出的答案不正确(并且如果
retry_on_fail is False
),循环将重复。 - 允许用户输入一些 "skip" 关键字来跳过问题。
- "test" 完成后显示结果。在这里,我们通过定义和调用
store.display_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()
现在,return 值目前没有任何作用,但它可以!
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])
快速而肮脏的解决方案可以使用默认值 "skip" 作为答案:
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()
但是jedwards给出的答案你真的要研究一下。有很多东西要学 程序设计。他不是授人以鱼,而是授人以渔。