Recursion in Python (factorial function)

前端 未结 2 1705
礼貌的吻别
礼貌的吻别 2021-01-28 20:48

I hope this is not too much of a stupid question, but why does the \'return 1\' statement in this Python code return the factorial of a number? This also happens for \'return Tr

2条回答
  •  深忆病人
    2021-01-28 21:44

    in python when you multiply a number by True it will operate like you are multiply by One, and when you multiply a number by False it will operate like when you multiply by Zero.

    so this is why you get a factorial af a number even if you use:

    return True
    

    instead of

    return 1
    

    but if you will call factorial(0) you will get True instead of 1.

提交回复
热议问题