Python 简单编程查询

Python 简单编程查询,python,Python,我需要在没有if..else构造的情况下执行这个逻辑 我们在实体、产品和类型中传递了3个值 def myfunc(Entity, Product, Type): if Type = Loan: return 0 elif Entity == 'ML', and Product in list ['BML', 'DDML']: return 1 return None 基本上,如果类型为贷款回报0, 如果Entity=ML,Product=BM

我需要在没有if..else构造的情况下执行这个逻辑 我们在实体、产品和类型中传递了3个值

def myfunc(Entity, Product, Type):
    if Type = Loan:
       return 0
    elif Entity == 'ML', and Product in list ['BML', 'DDML']:
       return 1
    return None
基本上,如果类型为贷款回报0, 如果Entity=ML,Product=BML或DDML,则返回1 对于所有其他场景,返回None 返回None作为一个fall-thru结果

return int(Type != Loan and Entity == 'ML' and Product in ['BML','DDML']`)

逻辑运算返回一个布尔值,然后可以将其转换为1或0,具体取决于布尔值是真是假。

为什么不能使用if语句?抱歉,失败的结果是无。请参阅更新的snipetIn在这种情况下,您将需要一个if语句。您不需要“else”,但如果程序中没有条件逻辑,就不能在一个条件下返回int,在另一个条件下也不能返回None。