'Why is my function to find the smallest word in an array not working?

I am writing a program to print out the smallest word in phrase. The output is supposed to give me the word "I" but it is instead printing out the word "am". How can I fix this? Please help, I am new to Python

#Given a string 'phrase' consisting of some words separated
# by some number of spaces, return the
# smallest word in the string.

def word_length(phrase):
    splitphrase = phrase.split(" ")

    min_word = ''
    for i, element in enumerate(splitphrase):
        if i < len(splitphrase)-1 and (len(element) <= len((splitphrase[i+1]))):
            min_word = element
    print(min_word)



word_length("hello I am Sam and am tall")


Solution 1:[1]

I'll put my code down below and then explain the changes I made:

def word_length(phrase):
    splitphrase = phrase.split(" ")

    min_word = splitphrase[0] #setting default value as first word
    for element in (splitphrase): #for each word in the list
        if len(element) < len(min_word): #if the word is shorter than our current min_word
            min_word = element #redefine min_word if the current word is shorter
    print(min_word)



word_length("hello I am Sam and am tall")

Output:

I

Similar to your code, we start by using the split() function to break our sentence up into words in a list.

To start finding the smallest word, we can define our min_word to initially be splitphrase[0] as our default value.

To determine if other words in the list are min_word, while iterating through every word in the list, if the length of the word we are iterating through in our list is less than the length of current min_word, we redefine min_word to be that current element.

I hope this helped! Let me know if you need any further help or clarification!

Solution 2:[2]

  1. the maximum word length possible is the length of the sentence
  2. I check that each word is smaller than the maximum length
  3. if it is smaller I reassign the value of min_word and the min_word_length
def word_length(phrase):
    splitphrase = phrase.split(" ")

    max_word_lenght = len(phrase)
    for el in splitphrase:
        if len(el) <= max_word_lenght:
            max_word_lenght = len(el)
            min_word = el
    print(min_word)



word_length("hello I am Sam and am tall")

If you are new to python I would recommend learning a debugging tool so you can better understand the flow of your program. If you use the command line a possibility is PDB (https://realpython.com/python-debugging-pdb/).

If you use IDE (Spyder, VS, PyTorch) they should have a debugger built-in

Solution 3:[3]

How about like this:

def word_length(phrase):
    sw = (words := phrase.split())[0]
    for word in words[1:]:
        if len(word) < len(sw):
            sw = word
    return sw
print(word_length("hello I am Sam and am tall"))

Note:

This will fail if phrase is an empty string or None. You will also need Python 3.8+

Solution 4:[4]

This code, just checks each word if its length if less than the previous shortest words length.

def word_length(phrase):
    splitphrase = phrase.split(" ") # Split the phrase into each individual word

    min_word = splitphrase[0] # Store the shortest word we've found yet
    for element in splitphrase: # Iterate over every word
        if len(element) < len(min_word): # Check if we haven't already set the min_word or this word is smaller than the current min_word
            min_word = element # Set min_word to the new shortest word

    print(min_word)

word_length('Hello how is it going today') # > is

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ani M
Solution 2
Solution 3
Solution 4