亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

只能將 str (而非列表)連接到 str

只能將 str (而非列表)連接到 str

一只萌萌小番薯 2023-07-18 15:40:58
我不斷收到“只能將 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 lyricsdef main():    sound = ["moo", "oink", "neigh", "cluck", "bahh"]    for animal in ["cow", "pig", "horse", "chick", "sheep"]:        print(verseFor(animal, sound))main()
查看完整描述

6 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

    所以基本上,在這段代碼中


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


查看完整回答
反對 回復 2023-07-18
?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

使用郵編


animals = ["cow", "pig", "horse", "chick", "sheep"]

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

for animal, sound in zip(animals, sounds):

  print(verseFor(animal, sound))


查看完整回答
反對 回復 2023-07-18
?
揚帆大魚

TA貢獻1799條經驗 獲得超9個贊

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"))


查看完整回答
反對 回復 2023-07-18
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

您必須將字符串連接到字符串。因此,要實現您想要做的事情,您必須像這樣重寫它:


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/中的教程


查看完整回答
反對 回復 2023-07-18
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

實際問題的答案是您需要使用 一起迭代兩個列表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 lyrics


def 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()


查看完整回答
反對 回復 2023-07-18
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

你可以用這個...


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():

    for animal,sound in zip(["cow", "pig", "horse", "chick", "sheep"],["moo", "oink", "neigh", "cluck", "bahh"]):

        print(verseFor(animal, sound))


main()


查看完整回答
反對 回復 2023-07-18
  • 6 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號