sentence = input()

# 去除逗号并分割句子为单词列表
words = sentence.replace(",", "").split()

# 初始化最长单词和最短单词
longest_word = ""
shortest_word = ""

# 遍历每个单词,更新最长单词和最短单词
for word in words:
    if len(word) > len(longest_word):
        longest_word = word
    if len(word) < len(shortest_word) or shortest_word == "":
        shortest_word = word

# 输出结果
print(longest_word)
print(shortest_word)

这段代码首先读取输入的句子,然后去除逗号并将句子分割成单词列表。接着,通过遍历每个单词,更新最长单词和最短单词。最后,输出最长单词和最短单词。

题目描述输入1行句子不多于200个单词每个单词长度不超过100只包含字母、空格和逗号。单词由至少一个连续的字母构成空格和逗号都是单词间的间隔。试输出第1个最长的单词和第1个最短单词入行句子输出两行输出第1行第一个最长的单词。第2行第一个最短的单词帮我写一串符合要求的 Python 代码

原文地址: http://www.cveoy.top/t/topic/igfd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录