遍歷整個列表:
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 添加冒號。