我有三个功能,#34;涓滴"并在另一个内部调用:
async def start_swings(self, server, user):
try:
while self.jailed[server.id][user.id]['current'] != 0:
await self.get_question(server, user)
else:
await self.escape_jail(server, user)
except KeyError: # User isn't jailed
pass
async def get_question(self, server, user):
rnd = random.choice(range(2))
if rnd == 0:
await self.swing_math(server, user)
elif rnd == 1:
await self.swing_reverse(server, user)
async def swing_math(self, server, user):
num1 = random.randint(1, 1000)
num2 = random.randint(1, 1000)
ops = ['+', '-']
op = random.choice(ops)
ans = int(eval(str(num1) + op + str(num2)))
await self.bot.send_message(user, "**\N{HAMMER} It's going to take you `{}` more swings to break free.**\n```What is {} {} {}?```\n*I might doze off if you take too long to answer. If you don't get a reply from me within a few seconds, type `>swing`. If you try to use `>swing` as a way to reset your current question, you'll get a cooldown penalty.*".format(self.jailed[server.id][user.id]['current'], num1, op, num2))
resp = await self.bot.wait_for_message(author=user)
resp = resp.clean_content
if resp == str(ans):
await self.bot.send_message(user, "Correct. \N{WHITE HEAVY CHECK MARK} Take a swing.")
self.jailed[server.id][user.id]['current'] -= 1
dataIO.save_json('data/jail/jailed.json', self.jailed)
elif resp == ">swing":
return
else:
await self.bot.send_message(user, "Wrong. \N{CROSS MARK} Let me put that brick back in place.")
self.jailed[server.id][user.id]['current'] += 1
dataIO.save_json('data/jail/jailed.json', self.jailed)
在第三个函数中,您将看到以下声明:
elif resp == ">swing":
return
我怎样才能有效地实现这一目标"双重回报"所以它改为start_swings
而不是get_questions
?我对Python很陌生,并且不知道该怎么称呼它。我有效地尝试执行此脚本以完全停止在代码的这个区域:
elif resp == ">swing":
return
...而不是让它返回到最后一个名为。
的函数答案 0 :(得分:3)
你不能像过去基本上那样手动“弹出”堆栈,但是你可以使用异常(即使没有抛出异常被认为是不好的做法“异常” ):
从异常继承:
class DoubleReturnException(Exception):
pass
安装处理程序:
while self.jailed[server.id][user.id]['current'] != 0:
try:
await self.get_question(server, user)
except DoubleReturnException:
pass
else:
然后不是回来,而是:
raise DoubleReturnException
返回处理程序的except
部分(无论调用的数量是多少)。
定义您自己的异常必需因为它确保程序因此而返回,而不是ValueError
或程序可能抛出的任何其他异常。
这必须是一个例外情况。通过在各处抛出异常进行编程会导致“意大利面条代码”并以某种方式恢复邪恶的goto
语句。你可能会发现在稳定产品的最后阶段很有趣,当你无法打破特定情况的所有(有缺陷的)设计时(但我个人从来没有使用过这样的技巧)。