Write a function isAllLettersUsed(word, required) that takes in a word as the first argument and returns True if the word contains all the letters found in the second argument.
Examples
>>> isAllLettersUsed('apple', 'apple')
True
>>> isAllLettersUsed('apple', 'google')
False
>>> isAllLettersUsed('learning python', 'google')
True
>>> isAllLettersUsed('learning python', 'apple')
True
What i'm Doing is
def isAllLettersUsed(word, required):
if all(required in word for required in word):
return True
else:
return False
And Result returning is
True
True
True
True
Where as it should return as
True
False
True
True
i don't understand what should i do at this point i have tried many things but failed. any suggestion??
Just see if all letters in required are in word:
def isAllLettersUsed(word, required):
return all(ch in word for ch in required)
You are checking if the each letter from word is in word by using required in your for loop, required refers to each character not the required parameter passed so it always returns True as every letter from word has to be in the word.
def isAllLettersUsed(word, required):
if all(req in word for req in required):
return True
else:
return False
What happened was that you were assigning a value to the variable required
in your for loop but required
is a parameter. You want to loop through required
and save each element of required
to a new variable. Then, check if that element is in word.
Additionally, your if-else
is redundant if you use all()
. Just say return all(...)
Change the name of your variables in your generator expression. You shadowed your argument named required
with your loop variable required
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.