【问题标题】:How to jump from an if statement to an else statement? [closed]如何从 if 语句跳转到 else 语句? [关闭]
【发布时间】:2022-06-10 23:08:19
【问题描述】:

假设我的代码是:

if str(file_exists).capitalize() == 'Flase':
    newFile = open(fr'{directory}\data\users.txt', 'w+')
    newFile.close()

else:
    (some code below...)

print(123)

那么在代码newFile.close()之后,我怎样才能直接跳转到else语句而不是直接跳转到print(123)呢?

【问题讨论】:

  • 听起来else 块根本不适合这项工作,您应该改用其他工具(例如try/finally)。或者如果你想无条件地跳转,那么代码完全没有理由放在else块中,你可以把它放在顶层。
  • 我敢打赌if str(file_exists).capitalize() == 'Flase' 应该是if not file_exists
  • 您能否提供一个示例来说明为什么您要这样做,以便我们可以建议一种替代方法来尝试解决基本推理?
  • 我想你没有得到我的问题.......
  • 我同意,我明白你的问题。你能edit说得更清楚吗?

标签: python