我正在尝试在Python 3.2中创建一个应用程序,我一直使用制表符进行缩进,但是即使编辑器也将其中的一些更改为空格,然后在我打印出“不一致使用制表符和空格”尝试运行该程序。
我是python编程的初学者。如果能在我的代码中获得一些总体提示,我将感到很高兴。
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DatTimeField(defult=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
答案 0 :(得分:1)
这仅表示您使用空格和制表符的组合来缩进代码,并且/或者空格/制表符的数量不一致(例如,有时您使用2,其他4个空格)。请记住,在Python中,缩进非常非常重要!例如,您的代码应如下所示:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DatTimeField(defult=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
使用良好的IDE或文本编辑器来避免此类错误,并在执行代码之前确保您的代码“漂亮”。在Python中使用适当的缩进不是可选的,这很棒!它迫使您编写易于他人阅读的代码。