python - 操作列表顺序
问题描述
我是一名艺术家,正在学习如何操作代码来创作诗歌。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...
有什么建议么?帮助将不胜感激。
解决方案
您只需要在循环内移动 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)
推荐阅读
- netsuite - 无法使用 ss2.0 动态创建 Restlet
- c++ - 使用 yaml-cpp 更新 YAML 文档的节点和值
- visual-studio-code - 如何为 Chrome(或 Edge)扩展设置调试器以正确断点在 wwwroot 之外提供的源文件?
- python - 代码未按预期打印碰撞时刻
- c# - 在 C# 中单击时,您可以将按钮变成图像吗?
- sql - select 子句中位置 16 'D_INS_COMPANY.ORDER_BY' 的错误行 1 既不是聚合也不是 group by 子句
- linux - awk、拆分和打印一系列列
- karate - 如何结合重试创建动态路径组件,直到
- c# - 点击 Geckofx 网络浏览器内的链接将触发 Winforms 中的方法
- android - Android WebView:手机锁定时音频断断续续