Search code examples
pythonvariablessentence

Associating the second word of a sentence to a variable. (Python)


I'm currently trying to perform this, but my book for class doesn't show anything in relation to Boolean variables or anything related to this problem. I've been looking online for a while and everything is way too complicated for something that should be simple according the unit. If the solution is posted here, what was typed in to find it? Maybe I need to rephrase my question.

https://i.sstatic.net/RMSEx.png


Solution

  • def getSecondWord(sentence):
        res = sentence.split(' ')[1]
        if res.endswith('.'):
            return res[:-1]
        else:
            return res
    
    inputSentence = 'broccoli is delicious'
    secondWord = getSecondWord(inputSentence)
    
    print("second word is:", secondWord)