在使用的时候会出现这种错误:
>>> def person(name, age, *, city, job):
... print (name,age,city,job)
File "<stdin>", line 2
print (name,age,city,job)
^
IndentationError: expected an indented block
这个错误么,其实比较常见,python和java不一样,譬如java的函数和选择循环等方法的内容都可以被{}给包起来,而python则不同,用的是缩进;
在command line中敲出def函数后回车是不会自动缩进的,在IDEL里会,so需要自己敲个空格缩进一下即可,如下:
>>> def person(name, age, *, city, job):
... print (name, age, city,job)
...
这样就不会报错了;
本文解析了Python中函数定义时常见的缩进错误,并对比了Python与Java在语法上的区别,详细介绍了如何正确地在Python中定义函数并避免缩进错误。
1万+

被折叠的 条评论
为什么被折叠?



