The Python Oracle

Function printing correct Output and None

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC M Beethoven - Piano Sonata No 3 in C 3

--

Chapters
00:00 Question
00:48 Accepted answer (Score 7)
01:40 Answer 2 (Score 5)
02:00 Answer 3 (Score 0)
02:31 Thank you

--

Full question
https://stackoverflow.com/questions/1246...

Accepted answer links:
[all]: http://docs.python.org/library/functions...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 7


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).




ANSWER 2

Score 5


Because uses_all() doesn't have a return statement. If you return a value from the function, it will be printed instead of None, unless of course you return None :)




ANSWER 3

Score 0


You print the result of the function, but the function doesn't return anything (--> None)

If you want the result to be False False, add return boolean in the function.

Alternatively, if a single False is enough, you could change print uses_all('facebook', 'd') to just uses_all('facebook', 'd')