Python 返回列表中最短单词的Write函数

Python 返回列表中最短单词的Write函数,python,list,function,Python,List,Function,我做了一个有效的函数。然而,输出与它应该的有点不同 在这个任务中,我不允许更改print语句,这意味着我必须调整函数以打印正确的结果。 我的职能: def shortest_word(word_list): shortest = word_list[0] shortest_lenght = len(shortest) for country in word_list: if shortest_lenght > len(country):

我做了一个有效的函数。然而,输出与它应该的有点不同

在这个任务中,我不允许更改print语句,这意味着我必须调整函数以打印正确的结果。 我的职能:

def shortest_word(word_list):
    shortest = word_list[0]
    shortest_lenght = len(shortest)
    for country in word_list:
        if shortest_lenght > len(country):
            shortest_lenght = len(country)
            shortest = country
    return shortest

word_list = ["denmark", "sweden", "germany"]
print(shortest_word(word_list)) 
它应该打印:
“瑞典”

但是它打印:
sweden
不带“

假设您不能更改
word\u列表本身,您可以使用
repr
如下所示

>>> word_list = ["denmark", "sweden", "germany"]
>>> print(word_list[1])
sweden
>>> print(repr(word_list[1]))
'sweden'
或者如果必须使用双引号

>>> print('"{}"'.format(word_list[1]))
"sweden"

字符串
“sweden”
不包含任何引号。如果您确实需要,请使用
word\u list=[““丹麦”、“瑞典”、“德国”]
(但我认为您不需要)。您不需要
shortest\u word
函数。只需执行
min(单词列表,key=len)
Thx即可获得反馈。深空。我正在做一门编程的入门课程,我们还没有读到min方法,所以我不认为我可以在这项作业中使用它,尽管它看起来更好。没问题。但我想问一下,为什么他们在印刷时要用双引号引这个名字?