Defines and prints shortest word in set, sorts the rest by length, and gives average word length I need to clean up and simplify this code (I am a beginner in Python):

print ("Type in four inputs.") 
word_first = raw_input("What is your first input? ") 
word_second = raw_input("What is your second input? ") 
word_third = raw_input("What is your third input? ") 
word_fourth = raw_input("What is your fourth input? ")

list_of_words = [word_first, word_second, word_third, word_fourth]
print
print sorted(list_of_words, key=len)

def average(numbers):
    return sum(numbers)/len(numbers)

all_words = word_first, word_second, word_third, word_fourth 
words_total = word_first, word_second, word_third, word_fourth.split()
length_words = [len(word) for word in words_total]

word_first_length = len(word_first)
word_second_length = len(word_second)
word_third_length = len(word_third)
word_fourth_length = len(word_fourth)
total_letters = word_first_length + word_second_length + word_third_length + 
word_fourth_length
average_length = total_letters / 4
print
print ("Shortest input is: ") + (min((word for word in list_of_words if word), key=len))
print ("Average length is %.1f.") %(average_length)
Up to, and including the line list_of_words, your code looks good. But then it gets confusing.

To summarize what your program is supposed to do:


4 times: read a word from the user
put all these words in a list
sort this list by the word length
print the first element of the list (which is the shortest)
take a break (in other words, no need to sort the rest of the list, since it is already sorted)
ask your teacher about the unclear specification:


the average word length of what exactly: the whole list, or the rest?
why should the rest of the words be sorted when there is no requirement to print them afterwards?



Your definition of the average function was already good, you just didn't use it.

In Python, the above instructions could look like:

words = [first_word, second_word, third_word, fourth_word]
sorted_words = sorted(words, key=len)
sorted_lengths = [len(word) for word in sorted_words]

print('Shortest word: {}'.format(sorted_words[0]))
print('Remaining words: {}'.format(sorted_words[1:]))
print('Longest word: {}'.format(sorted_words[-1]))
print('Average word length: {}'.format(average(sorted_lengths)))
print('Average remaining word length: {}'.format(average(sorted_lengths[1:])))


Basically, you already had all the ingredients for solving the problem, you just mixed them up a bit.