【问题标题】:Move to else statement based on variable update outside if-else statement基于 if-else 语句外的变量更新移动到 else 语句
【发布时间】:2022-08-16 17:37:14
【问题描述】:

伙计们,这可能是一个小问题,但我被困在这里,任何人都可以帮助我理解我的要求,将不胜感激。

check = True
if check:
    for i in dict:
        if i in another_dict:
            do something
        else:
            check = False

else:
    do something

最初,我将 True(bool) 值分配给 IF-ELSE 语句外部的变量,如果我从 IF 语句内部更新相同的变量,我想移动到 else 语句。

(即)在上面的代码中, 如果变量 \"check\" = False,我想继续使用 main else 语句

    标签: python if-statement


    【解决方案1】:

    只需使用另一个if

    check = True
    if check:
        for i in dct:
            if i in another_dict:
                do_something()
            else:
                check = False
    
    if check:
        do_something()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 2018-01-09
      • 2015-07-06
      • 2012-08-05
      相关资源
      最近更新 更多