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

Unexpected Indent ใน Python คืออะไร

Python ต้องการ indentation consistent หากบรรทัดหนึ่ง indent 4 spaces บรรทัดถัดไป indent 3 หรือ 5 จะได้ IndentationError: unexpected indent

ตัวอย่างผิด

def a():
    print "foo"
     print "baz"  # indent ไม่เท่ากัน

ตัวอย่างถูกต้อง

def a():
    print "foo"
    print "baz"

ทุกบรรทัดใน block ต้อง indent เท่ากันทุกบรรทัด