There’s a lot of repetition in your code. I assume all you’re trying to do is get four words as input, make a list of them, sort them based on their lengths, find the length of each word, the number of characters in all these words and average of the lengths of the words, finally print the shortest word and the average length.
print ("Type in four inputs.")
You could be more specific:
print("Type in four words separated by a space.")
Get a list of four words with one raw_input()
list_of_words = raw_input('> ').split()
If you want a tuple of words, you could convert a list to a tuple:
all_words = tuple(list_of_words)
If you're trying to put all the words into a list, you could remove
words_total = word_first, word_second, word_third, word_fourth.split()
because you already have a list of the words above.
Note all_words
and words_total
are almost similar except the former is a tuple
and the latter a list
. Decide if you want them both.
length_words = [len(word) for word in words_total]
I'm changing words_total
in the above to list_of_words
and
word_first_length = len(word_first)
word_second_length = len(word_second)
word_third_length = len(word_third)
word_fourth_length = len(word_fourth)
I would have the above four values in a dictionary so you've pairs of words and their lengths.
lengths_of_words = {word: len(word) for word in list_of_words}
Now that I've a dictionary above, I could sum its values.
total_letters = sum(lengths_of_words.values())
Instead of the constant divisor 4, use len(list)
to let Python get the divisor for you:
average_length = total_letters / len(list_of_words)
There's already a list above, so the generator in this print()
is not needed.
print("Shortest input is: ") + (min((word for word in list_of_words if word), key=len))
That can be changed to:
print("Shortest input is: " + min(list_of_words, key=len))
I can't see average(numbers)
function used anywhere in your code.
print("Average length is %.1f.") %(average_length)
You get a TypeError
with the above, so rewrite it as:
print("Average length is %.1f." % average_length)
I agree with @Coal_'s suggestion: If you're starting out with Python, learn Python 3. For advice on choosing between Python 2 and Python 3, see Python 2 or 3.
That said, I would rewrite your code, like so:
list_of_words = raw_input("Type in four words separated by a space: ").split()
# Sort the words by their lengths and print them
print("\nSorting the words by their lengths and printing them:")
print(sorted(list_of_words, key=len))
# Create an empty dictionary
lengths_of_words = {}
# Loop through the list and add each word and its length to the above dictionary
for word in list_of_words:
lengths_of_words[word] = len(word)
# With dict comprehension, it can be written in a one-liner:
# lengths_of_words = {word: len(word) for word in list_of_words}
total_letters = sum(lengths_of_words.values())
average_length = total_letters / len(list_of_words)
print("\nThe shortest word is: " + min(list_of_words, key=len))
print("Average length is {}.".format(average_length))
Running it:
Type in four words separated by a space: Python language is popular
Sorting the words by their lengths and printing them:
['is', 'Python', 'popular', 'language']
The shortest word is: is
Average length is 5.75.
>>>
You can visualize its execution in Python 2.7 at Python Tutor