【问题标题】:Python order of multiple elif statements多个elif语句的Python顺序
【发布时间】:2020-05-14 13:12:30
【问题描述】:

在 python 中,多个“elif”语句的顺序是否重要,特别是在字符串检查中

例如:

if 'foo' in line:
  #do something
elif 'bar' in line:
  #do other thing
elif 'coffe' in line:
  #do other other thing
...
else:
  #something something

【问题讨论】:

  • 是的。如果一个 elif 为 True,则后续的将无关紧要。

标签: python if-statement


【解决方案1】:

是的,if-else 语句是按顺序执行的,这意味着 python 将运行 if 语句,如果它是 false,那么它将运行下一个 elif,依此类推,直到 else 语句仅在以上所有条件都执行时才执行假的。

【讨论】:

猜你喜欢
  • 2015-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 2019-01-17
  • 2020-10-28
相关资源
最近更新 更多