Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Đặt comment ở đâu trong cấu trúc if...elif..else?

Comment có thể đặt ở bất kỳ đâu: trước mỗi block hoặc bên trong block. Lưu ý: Không đặt multiline comment (triple quotes) trước elif và else vì chúng được coi là string literal, làm ngắt cấu trúc.

# If check
if True:
    # Another Comment style
    print("If block")
# Else if statement
elif False:
    # Another Comment style
    print("elif block")
# Else
else:
    # Another Comment style
    print("Else block")

Kết quả: If block