Python3.3 版本: 系统报错inconsistent use of tabs and spaces in indentation

class HotDog:

def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.cooked_condiments = []

def cook(self, time):
self.cooked_level = self.cooked_level + time
if self.cooked_level > 8:
self.cooked_string = "Charcoal"
elif self.cooked_level > 5:
self.cooked_string = "Well-done"
elif self.cooked_string > 3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"

系统报错,是缩进的问题吗?看上去是一样的

建议用一个集成IDE编辑器,推荐pycharm,它会检测到代码缩进问题,并提供自动缩进的功能。

Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中。最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分别的。

在编译时会出现这样的错IndentationError:expected an indented block说明此处需要缩进,你只要在出现错误的那一行,按空格或Tab(但不能混用)键缩进就行。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-08-25
使用tab和空格用错误
python是严格按照缩进来控制语法的,所以报了syntax error
这个一般是小问题,调整一下编辑器的设置就可以了
第2个回答  推荐于2018-03-12

我试运行下,没有报错啊,估计是你缩进问题吧。

class HotDog:
    def __init__(self):
        self.cooked_level = 0
        self.cooked_string = "Raw"
        self.cooked_condiment = []

    def cook(self,time):
        self.cooked_level = self.cooked_level + time
        if self.cooked_level > 8:
            self.cooked_string = "Charcoal"
        elif self.cooked_level > 5:
            self.cooked_string = "Well-done"
        elif self.cooked_level > 3:
            self.cooked_string = "Medium"
        else:
            self.cooked_string = "Raw"

本回答被网友采纳
第3个回答  2018-03-11
inconsistent use of tabs and spaces in indentation
这个报错就是混用了tab和4个空格造成的,检查代码,要不全部用tab,要不全部用4个空格,或者用idle编辑器校正
第4个回答  2015-09-08
代码段的缩进不一致,调整一下缩进