猿问

只能将 str (而非列表)连接到 str

我不断收到“只能将 str (不是列表)连接到 str”,但我不确定为什么会收到此错误。我对 python 相当陌生,所以任何帮助将不胜感激。


def oldMacdonald():

    return "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!\n"


def verseFor(animal, sound):

    lyrics = oldMacdonald() + "And on his farm he had a " + animal + ", Ee-igh, Ee-igh, Oh!\n" \

        "With a " + sound + ", " + sound + " here and a " + sound + ", " \

        "" + sound + ".\nHere a " + sound + ", there a " + sound + ", " \

        "everywhere a " + sound + ", " + sound + "\n" + oldMacdonald()


    return lyrics


def main():

    sound = ["moo", "oink", "neigh", "cluck", "bahh"]

    for animal in ["cow", "pig", "horse", "chick", "sheep"]:

        print(verseFor(animal, sound))


main()


一只萌萌小番薯
浏览 124回答 6
6回答

红颜莎娜

    所以基本上,在这段代码中sound = ["moo", "oink", "neigh", "cluck", "bahh"]    for animal in ["cow", "pig", "horse", "chick", "sheep"]:        print(verseFor(animal, sound))sound是一个列表并且animal正在迭代列表,即动物是列表的单个元素,意味着cow在第一次迭代、pig第二次、horse第三次等等。但是您sound在 中传递的是整个列表,而不是其中的单个元素verseFor。因此,您必须迭代两个列表,以逐个元素发送它们的动物和声音。如前所述,您可以zip像这样使用。sound = ["moo", "oink", "neigh", "cluck", "bahh"]animal = ["cow", "pig", "horse", "chick", "sheep"]for ani, sou in zip(animal, sound):    print(verseFor(ani, sou))现在您正在循环播放声音和动物元素。如果你查看 的输出zip,你会得到这个。list(zip(animal,sound))>>>[('cow', 'moo'), ('pig', 'oink'), ('horse', 'neigh'), ('chick', 'cluck'), ('sheep', 'bahh')]所以基本上在我提供的代码的第一次迭代中,我们传入cow和。然后在下一次迭代中分别进行和,依此类推。animoosoupigoink

慕田峪9158850

使用邮编animals = ["cow", "pig", "horse", "chick", "sheep"]sounds = ["moo", "oink", "neigh", "cluck", "bahh"]for animal, sound in zip(animals, sounds):  print(verseFor(animal, sound))

扬帆大鱼

def main():    sound = ["moo", "oink", "neigh", "cluck", "bahh"]    for animal in ["cow", "pig", "horse", "chick", "sheep"]:        print(verseFor(animal, sound))问题出在声音字典上。这是一个快速修复:def main():    animals = [ ["cow", "moo"], ["pig", "neigh"], ["sheep", "bahh"] ]    for animal in animals:        print(verseFor(animal[0], animal[1]))或者你可以使用这个方法:def main():    animals = [        {            "name" : "cow",            "sound": "moe"        },        {            "name" : "pig",            "sound": "haha"        },        {            "name" : "dog",            "sound": "lol"        }    ]    for animal in animals:        print(verseFor(animal["name"], animal["sound"))

MMTTMM

您必须将字符串连接到字符串。因此,要实现您想要做的事情,您必须像这样重写它:lyrics = oldMacdonald() + "And on his farm he had a " + animal + ", Ee-igh, Ee-igh, Oh!\n" \        "With a " + sound[0] + ", " + sound[1] + " here and a " + sound[2] + ", " \        "" + sound[3] + ".\nHere a " + sound[4] + ", there a " + sound[5] + ", " \        "everywhere a " + sound[6] + ", " + sound[7] + "\n" + oldMacdonald()但是,还有一个额外的问题:您只有 5 种动物及其 5 个相应的声音,但您在歌词中放置了 8 个声音......!所以我们在歌词上添加了至少 3 个额外的“声音”。您可能想查看网络上优秀的 Python 教程之一,例如官方 Python 网站https://docs.python.org/3/tutorial/中的教程

qq_花开花谢_0

实际问题的答案是您需要使用 一起迭代两个列表zip。然而,单独来看,它打印出“a oink”令我烦恼。这是一个将打印“an oink”的版本——该a函数返回一个前面带有适当的不定冠词(“a”或“an”)的单词。def oldMacdonald():    return "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!"def a(thing):    if thing[0] in 'aeiou':        return f'an {thing}'    else:        return f'a {thing}'def verseFor(animal, sound):    an_animal = a(animal)    a_sound = a(sound)    lyrics = f"""{oldMacdonald()}And on his farm he had {an_animal}, Ee-igh, Ee-igh, Oh!With {a_sound}, {sound} here and {a_sound}, {sound} there.Here {a_sound}, there {a_sound}, everywhere {a_sound}, {sound}.{oldMacdonald()}"""    return lyricsdef main():    sounds = ["moo", "oink", "neigh", "cluck", "bahh"]    animals = ["cow", "pig", "horse", "chick", "sheep"]    for animal, sound in zip(animals, sounds):        print(verseFor(animal, sound))main()

跃然一笑

你可以用这个...def oldMacdonald():    return "Old MacDonald had a farm, Ee-igh, Ee-igh, Oh!\n"def verseFor(animal, sound):    lyrics = oldMacdonald() + "And on his farm he had a " + animal + ", Ee-igh, Ee-igh, Oh!\n" \                                                                        "With a " + sound + ", " + sound + " here and a " + sound + ", " \                                                                                                                                             "" + sound + ".\nHere a " + sound + ", there a " + sound + ", " \                                                                                                                                                                                                                 "everywhere a " + sound + ", " + sound + "\n" + oldMacdonald()    return lyricsdef main():    for animal,sound in zip(["cow", "pig", "horse", "chick", "sheep"],["moo", "oink", "neigh", "cluck", "bahh"]):        print(verseFor(animal, sound))main()
随时随地看视频慕课网APP

相关分类

Python
我要回答