Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วาง comment ใดใน if...elif..else construct

คุณสามารถวาง comment ได้ที่ไหนก็ได้ใน if...elif...else statement เช่น ก่อนบล็อกแต่ละบล็อก หรือภายในบล็อกแต่ละบล็อก โปรดทราบว่าไม่สามารถวาง multiline comments ก่อน elif และ else ได้ เนื่องจาก comments เหล่านี้เป็น strings จริงๆ ซึ่งหมายถึงการหยุดชะงักของทั้ง construct ตัวอย่างเช่น

# 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")

จะได้ผลลัพธ์:

If block