If there is no word contains "a" or "A" in a sentence, print -1.
Here is my code, I can print the words if they contain "a" or "A", but if not I can’t manage to print -1 out.
Where should I change?
def newsentence ():
letters = set('aA')
new_ls = [word for word in sentence if letters & set(word)]
return new_ls
def findlongest ():
if 'a' or 'A' in sentence:
longest = max(newsentence(), key=len)
else:
longest== -1
return(longest)
sentence = list(input().split())
print(findlongest())
>Solution :
The newsentence will return an empty array if there is no a or A. Please change the code to below. It should work. As @Tomer said, you also need to change == to = as well
def newsentence ():
letters = set('aA')
new_ls = [word for word in sentence if letters & set(word)]
return new_ls
def findlongest ():
if 'a' or 'A' in sentence:
if newsentence():
longest = max(newsentence(), key=len)
else:
longest = -1
return(longest)
sentence = list(input().split())
print(findlongest())