我是一名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)
答案 0 :(得分:0)
尝试使用for循环。
min = 100000000
total = 0
repeat = 4
for i in range(repeat):
len_input = len(input("Number: "))
if len_input < min:
min = len_input
total += number
print(min)
print(total/number)
关于for循环的一些说明
range(0, 30, 5) => [0, 5, 10, 15, 20, 25]
[word for word in list_of_words]
也会起作用。