Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python encoding/decoding of str, methods

Given a sentence string. Write the shortest word in a sentence. If there are several such words, then output the last one. A word is a set of characters that does not contain spaces, punctuation marks and is delimited by spaces, punctuation marks, or the beginning/end of a line.

Input: sentence = “I LOVE python version three and point 10”

Output: "I"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My attempt:

sentence = input("sentence: ")
words = sentence.split()
min_word = None
for word in words:
    if len(word) < len(words):
        min_word = word
print(min_word)

But output is : 10

Can you help me?

>Solution :

this bug because of if len(word) < len(words):. It can be if len(word) < len(min_word): and to fix len(None) you can use this code:

sentence = input("sentence: ")
words = sentence.split()
min_word = words[0]
for word in words:
    if len(word) < len(min_word):
        min_word = word
print(min_word)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading