首页 > 解决方案 > 操作列表顺序

问题描述

我是一名艺术家,正在学习如何操作代码来创作诗歌。Python 不应该是先决条件,但我不明白!请帮忙——我们应该写一首雪球诗。这是我到目前为止的代码:

my_string = "you're going home in a Chelsea ambulance"

counter = 0

new_list = my_string.split()

def make_a_snowball (text):
    poem = ' '
    for i in text:
        poem = poem + "\n" + i 
    print (poem)

make_a_snowball (new_list)

结果是:

you're
going
home
etc..

我希望它看起来像:

you're
you're going
you're going home 
etc...

有什么建议么?帮助将不胜感激。

标签: python

解决方案


您只需要在循环内移动 print 方法:

my_string = "you're going home in a Chelsea ambulance"

counter = 0

new_list = my_string.split()
print(new_list)

def make_a_snowball(text):
    poem = ' '
    for word in text:
        poem = poem + ' ' + word
        print(poem)


make_a_snowball(new_list)

推荐阅读