遍历整个列表:
For 循环:
- 使用列表项自动执行重复性任务。
- 示例: for magician in magicians: print(magician) .
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
>>
alice
david
carolina
迭代所有项:
- Loop 检索并处理每个项目。
- 示例:在列表中打印每个魔术师的名字。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
>>
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
循环执行:
- 对每个列表项重复上述步骤。
- 示例:使用循环的个性化消息。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
>>
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
循环后操作:
- 循环后的代码执行一次。
- 示例:在单独消息后感谢所有魔术师。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you, everyone. That was a great magic show!")
>>
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you, everyone. That was a great magic show!
避免 Python 中的缩进错误
忘记缩进:
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician) # Should be indented
#Error :
File "magicians.py", line 3
print(magician)
^
IndentationError: expected an indented block after 'for' statement on line 2
- 错误: IndentationError: expected an indented block after 'for' statement
- 修复:缩进 print(magician) 行。
忘记缩进额外的行:
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n") # Should be indented
#OutPut
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
- 问题:只有最后一个魔术师会收到第二条消息,因为该行不在循环内。
- 修复:缩进第二个 print 语句。
不必要地缩进:
message = "Hello Python world!"
print(message) # Unnecessary indent
#Error :
File "hello_world.py", line 2
print(message)
^
IndentationError: unexpected indent
- 错误: IndentationError: unexpected indent
- 修复:删除不必要的缩进。
在循环后缩进:
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you everyone, that was a great magic show!") # Should not be indented
>>
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
Thank you everyone, that was a great magic show!
David, that was a great trick!
I can't wait to see your next trick, David.
Thank you everyone, that was a great magic show!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you everyone, that was a great magic show
- 问题:感谢信息是为每位魔术师打印的。
- 修复:取消缩进最后一个 print 语句。
忘记冒号:
for magician in magicians # Missing colon
print(magician)
#Error :
File "magicians.py", line 2
for magician in magicians
^
SyntaxError: expected ':'
- 错误:语法错误:应为“:”
- 修复:在 magicians 中为 magician 添加冒号。