Function printing correct Output and None

后端 未结 3 682
借酒劲吻你
借酒劲吻你 2021-01-20 22:33

I have defined this function that takes a word and a string of required letters and that returns True if the word uses all the required letters at least once. When I run thi

3条回答
  •  有刺的猬
    2021-01-20 23:26

    Your program prints boolean, which is False, so you know where that comes from.

    If a function doesn't return anything explicitly, it automatically returns None, and when you use

    print uses_all('facebook', 'd')
    

    you're asking it to print what uses_all returns, which is None. Hence:

    False
    None
    

    BTW, I think your function could be more concisely written as

    def uses_all(word, allused):
        return all(e in word for e in allused)
    

    Could make it more efficient, but that should be good enough for government work. The all function is really handy (see also any).

提交回复
热议问题