英文:
Curious why print statement is executed with every line, when the goal is only to have it print once, at the bottom of the list
问题
requested_trades = ['仅做多', '仅做空', '中性仓位']
for requested_trade in requested_trades:
print(f"我的交易清单: {requested_trade}。")
print("只在底部出现“执行良好”。")
英文:
Code below:
requested_trades = ['long only', 'short only', 'neutral position']
for requested_trade in requested_trades:
print(f"My Trade List: {requested_trade}.")
print("\nExecute them well")
The output:
> lang-none
> My Trade List: long only.
>
> Execute them well
> My Trade List: short only.
>
> Execute them well
> My Trade List: neutral position.
>
> Execute them well
>
I was under the belief "Execute them well" should only appear at the bottom line.
答案1
得分: 1
Python不使用花括号或关键词如do/end
来定义块。它使用空白缩进。在相同级别缩进的任何内容都属于该块,即使你插入一个空白行也一样。
以下内容将实现你的期望:
requested_trades = ['long only', 'short only', 'neutral position']
for requested_trade in requested_trades:
print(f"My Trade List: {requested_trade}.")
print("执行它们很好")
英文:
Python doesn't use curly braces or keywords such as do/end
to define blocks. It uses white-space indentation. Anything indented at the same level is part of that block, even if you put in a blank newline.
The following will do what you expect:
requested_trades = ['long only', 'short only', 'neutral position']
for requested_trade in requested_trades:
print(f"My Trade List: {requested_trade}.")
print("\nExecute them well")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论