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

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

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

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

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

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()
添加回答
舉報