问题是,当我有一个句子,其中包含两个单词的字母数相同时,如何让程序在阅读时给我第一个最长的单词,而不是两个单词?在
import sys
inputsentence = input("Enter a sentence and I will find the longest word: ").split()
longestwords = []
for word in inputsentence:
if len(word) == len(max(inputsentence, key=len)):
longestwords.append(word)
print ("the longest word in the sentence is:",longestwords)
例子:快速棕色狐狸…现在程序给我“快速”和“棕色”,如何调整我的代码,使我只给“快”从第一个最长的单词?在
只需打印列表中的第一个:
可能有更好的方法来实现这一点,但这需要对代码进行最少的修改。在
为什么不只是:
我会完全摆脱for循环,只需这样做:
相关问题 更多 >
编程相关推荐