The task is to check if the sentence contains all letters of the alphabet. So while you can determine that is not the case (i.e. return false
) when you find the first letter that is not in the sentence, you cannot state the opposite (i.e. return true
) until you have checked all the letters.
def is_pangram(sentence):
alf = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
sentence = sentence.lower()
for x in alf:
if x not in sentence:
return False
return True
Addendum:
There is also the python-only, little known and seldom used for/else
language feature see docs: for
loops may have an else
clause which is called when the loop exits "normally" (i.e. with out stopping early due to a break
, return
or exception). This makes the following code a viable alternative. Note the different indentation of the else
clause when compared with the OP code.
def is_pangram(sentence):
alf = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
sentence = sentence.lower()
for x in alf:
if x not in sentence:
return False
else:
return True