Saya tidak mengerti mengapa python memberikan "Expected indentation block" error?
""" This module prints all the items within a list"""
def print_lol(the_list):
""" The following for loop iterates over every item in the list and checks whether
the list item is another list or not. in case the list item is another list it recalls the function else it prints the ist item"""
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item)
else:
print(each_item)
Anda harus mengindentasi docstring setelah definisi fungsi di sana (baris 3, 4):
def print_lol(the_list):
"""this doesn't works"""
print 'Ain't happening'
Indentasi:
def print_lol(the_list):
"""this works!"""
print 'Aaaand it's happening'
Atau anda bisa menggunakan #
untuk berkomentar:
def print_lol(the_list):
#this works, too!
print 'Hohoho'
Juga, Anda dapat melihat PEP 257 tentang docstrings.
Semoga ini membantu!
Saya juga mengalami hal itu misalnya:
Kode ini tidak bekerja dan mendapatkan kesalahan blok yang dimaksud.
class Foo(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField()
def __unicode__(self):
return self.title
Namun, ketika saya menekan tab sebelum mengetikkan pernyataan return self.title, kode tersebut berfungsi.
class Foo(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField()
def __unicode__(self):
return self.title
Semoga ini bisa membantu yang lain.