1

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())
S3DEV
  • 8,768
  • 3
  • 31
  • 42
Jwencar
  • 51
  • 3
  • 1
    You're comparing in `else` block i.e. `longest== -1`. Instead of this assign -1 to longest i.e. `longest = -1` – Avinash Apr 20 '22 at 07:14
  • Please update your question with some sample inputs and your actual outputs. – quamrana Apr 20 '22 at 07:21
  • The reason the `-1` statement is not executing is because the `if` statement *always* evaluates to `True`, as explained [here](https://stackoverflow.com/a/71936409/6340496). – S3DEV Apr 20 '22 at 08:53

3 Answers3

4

Change longest== -1 to longest = -1, this should solve the problem.

Another problem will be the If statement which is not written properly. change to the following:

def findlongest (sentence):
    if any(['a' in word for word in sentence]) or any(['A' in word for word in sentence]):
        longest = max(newsentence(), key=len)
    else:
        longest = -1
    return(longest)
Tomer Geva
  • 1,764
  • 4
  • 16
2

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 newsentence():
        longest = max(newsentence(), key=len)
    else:
        longest = -1
    return(longest)

sentence = list(input().split())
print(findlongest())

Redox
  • 9,321
  • 5
  • 9
  • 26
  • 1
    The first `if` in `findlongest()` is completely redundant. – quamrana Apr 20 '22 at 07:28
  • Thanks @quamrana ... you are right, removed the first IF – Redox Apr 20 '22 at 08:20
  • Yes, the first `if` was also redundant because the `newsentence()` function does the same thing. (btw you shouldn't be in the habit of calling a function twice when calling it once and saving the return value would work) – quamrana Apr 20 '22 at 08:32
0

This can be achieved very simply, as shown below:

max((x for x in sentence.split() if 'a' in x.lower()), key=len)

The sentence is split into words, where each word is tested for the lowercase 'a', as all words in the sentence are lower cased. Then the longest word based on length is returned.


As a function:

def findlongest(sentence: str):
    try:
        return max((x for x in sentence.split() if 'a' in x.lower()), key=len)
    except ValueError:
        return -1

Examples:

>>> findlongest('A longer word and a longA word.')
'longA'

>>> findlongest('A sentence with only one outcome.')
'A'

>>> findlongest('No words to return.')
-1

Explanation why the original code does not work as expected

The statement if 'a' or 'A' in sentence will always evaluate to True, via 'truthiness' logic. Therefore, it's not functioning as you think it is. The statement is actually evaluating as:

if ('a') or ('A' in sentence)

As a test, try 'a' or 2 in 'bob' and you'll see.

S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • Your `"in progress"` seems to be taking a long time. I find I get downvotes if I try this. – quamrana Apr 20 '22 at 08:34
  • @quamrana - Oops! Alll done. Thanks mate. – S3DEV Apr 20 '22 at 08:38
  • Ok, I see what you did. (Personally I don't approve of using exceptions as flow control, but I think I'm in a minority or maybe its just python) – quamrana Apr 20 '22 at 08:43
  • @quamrana - *Generally*, I absolutely agree. I used them here to keep the logic as simple and transparent as possible. 'Time and place ...' – S3DEV Apr 20 '22 at 08:48