我是 python 的初学者,我有一个任务,我需要使用明确的循环、字符串累加器和连接打印出一首歌曲。问题是,我能够在一个明确的循环中打印出每个节(这首歌假设一首 3 节歌曲,因此范围设置为 3),并且在创建每个节之前,它要求用户输入一个动物,它是声音(它的老麦克唐纳)。我完成了任务的第一部分,即在用户提供输入后打印每个节,但第二部分要求将所有节(总共 3 个)连接到整首歌曲中。所以最终的结果是把单独的小节放在一首歌曲中。问题是,鉴于我必须更新歌曲然后在最后输出整首歌曲,我该如何使用累加器?附上我的代码:
def main():
for stanza in range(3):
animal = raw_input("What animal? ")
animalSound = raw_input("What sound does a %s make? " %(animal))
print
print "\t Old MacDonald had a farm, E-I-E-I-O,"
print "And on his farm he had a %s, E-I-E-I-O" %(animal)
print "With a %s-%s here - " %(animalSound, animalSound)
print "And a %s-%s there - " %(animalSound, animalSound)
print "Here a %s there a %s" %(animalSound, animalSound)
print "Everywhere a %s-%s" %(animalSound, animalSound)
print "Old MacDonald had a farm, E-I-E-I-O"
print