我有这个代码来自django示例教程
from django.db import models
from datetime import datetime
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
Run Code Online (Sandbox Code Playgroud)
我收到此错误:IndentationError:意外缩进
在这条线上:
def __unicode__(self):
Run Code Online (Sandbox Code Playgroud)
任何想法有什么不对?
我在下面编写了这个脚本,它将数字转换成它的拼写.
no = raw_input("Enter a number: ")
strcheck = str(no)
try:
val = int(no)
except ValueError:
print("sayi degil")
raise SystemExit
lencheck = str(no)
if len(lencheck) > 6:
print("Bu sayi cok buyuk !")
raise SystemExit
n = int(no)
print(n)
def int2word(n):
n3 = []
r1 = ""
ns = str(n)
for k in range(3, 33, 3):
r = ns[-k:]
q = len(ns) - k
if q < -2:
break
else:
if q >= 0:
n3.append(int(r[:3]))
elif q >= -1:
n3.append(int(r[:2]))
elif …Run Code Online (Sandbox Code Playgroud) 我已经尝试过notepad ++和eclipse但是即便如此,它在第18行显示了一个缩进错误.我不知道,为什么它会给我一个像这样的错误......?请帮我.
from brisa.core.reactors.qtreactor import QtReactor
reactor = QtReactor()
from brisa.core import config
from brisa.upnp.device import Device
from brisa.upnp.device.service import Service, StateVariable
class QtDevice(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.title = QtGui.QLabel("Qt Simple Device")
font = QtGui.QFont()
font.setPointSize(15)
self.title.setFont(font)
self.title.setAlignment(QtCore.Qt.AlignCenter)
self.verticalLayout.addWidget(self.title)
self.lineEdit = QtGui.QLineEdit(self)
self.verticalLayout.addWidget(self.lineEdit)
self.search_btn = QtGui.QPushButton("Start Device", self)
self.verticalLayout.addWidget(self.search_btn)
QtCore.QObject.connect(self.search_btn, QtCore.SIGNAL("clicked()"), self.start)
self.stop_btn = QtGui.QPushButton("Stop Device", self)
self.verticalLayout.addWidget(self.stop_btn)
QtCore.QObject.connect(self.stop_btn, QtCore.SIGNAL("clicked()"), self.stop)
self.lineEdit.setText(’My Generic Device Name’)
self.root_device = None
self.upnp_urn = ’urn:schemas-upnp-org:device:MyDevice:1’
def _add_root_device(self):
project_page = ’http://brisa.garage.maemo.org’
serial_no …Run Code Online (Sandbox Code Playgroud) 我的缩进有什么问题?
>>> try:
print("Hello World!")
except:
File "<pyshell#2>", line 3
except:
^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)
使用IDLE Python 2.7.2,Shell; 在try语句之后,我尝试退格到正确的缩进,但它转到左边距,我必须键入4个空格.
list=['a','a','x','c','e','e','f','f','f']
i=0
count = 0
while count < len(list)-2:
if list[i] == list[i+1]:
if list [i+1] != list [i+2]:
print list[i]
i+=1
count +=1
else:print "no"
else:
i +=1
count += 1
Run Code Online (Sandbox Code Playgroud)
我越来越:
else:print "no"
^
IndentationError: unexpected indent
Run Code Online (Sandbox Code Playgroud)
我试图只打印匹配以下元素的elts,但不打印后面的元素.我是Python的新手,我不确定为什么这不起作用.
我正在尝试解析XML文档并获取某些标记.我想获取名称标签(仅当它是嵌套在艺术家中的名称标签)和标题标签(仅当它是嵌套在发布中的标签).
这不是太重要,但重要的是,我出于某种原因得到一个错误,说elif语句是无效的语法
我已查看其他帖子并确保我的标签是正确的,并且没有任何任何if之后的额外换行符.
这是代码片段:
from lxml import etree
import sys
#infile = raw_input("Please enter an XML file to parse: ")
outfile = open('results.txt', 'a')
path = []
for event, elem in etree.iterparse('releases7.xml', events=("start", "end")):
if event == 'start':
path.append(elem.tag)
elif event == 'end':
# process the tag
if elem.tag == 'name':
if 'artist' in path and not 'extraartists' in path and not 'track' in path:
outfile.write( 'artist = ' + elem.text.encode('utf-8') + '\n' )
elif elem.tag == 'title':
if 'release' …Run Code Online (Sandbox Code Playgroud) 鉴于以下解释器会话:
>>> def func(depth,width):
... if (depth!=0):
... for i in range(width):
... print(depth,i)
... func(depth-1,width)
File "<stdin>", line 5
func(depth-1,width)
^
TabError: inconsistent use of tabs and spaces in indentation
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我TabError我的代码是什么?
我正在使用Python 2.7并编写了以下内容:
def算术(A):
x = 1
“”
一些评论在这里
“”
如果x = 1:
x = 1
elif x = 2:
x = 2
返回0
但是它有缩进的问题:
如果x = 1:
^
IndentationError:意外缩进
那么如何在函数中写注释呢?
我在解释器提示符下尝试了这段代码:
>>> x = 7
>>> if x > 5:
... print("five")
Run Code Online (Sandbox Code Playgroud)
我收到此错误消息:
File "<stdin>", line 2
print("five")
IndentationError: expected an indented block
Run Code Online (Sandbox Code Playgroud)
为什么?
我的IndentationError看起来似乎无法解决. http://pastebin.com/AFdnYcRc.
#!/usr/bin/env python
import os
import glob
import shutil
import mutagen
from sys import exit
musicdir = raw_input("What directory are the music files located in? : ")
musfile = glob.glob(musicdir + '/' + "*.mp3")
musfile1 = glob.glob(musicdir + '/' + "*.flac")
musfile.extend(musfile1)
newmusicdir = raw_input("What directory should the music files be organized into? : ")
done = False
while not done:
for m in musfile:
if musfile:
try:
musta = mutagen.File(m, easy=True)
mar = str(musta['artist'][0])
mal = str(musta['album'][0]) …Run Code Online (Sandbox Code Playgroud) python ×10
indentation ×2
python-2.7 ×2
django ×1
exception ×1
python-3.3 ×1
python-3.x ×1
shell ×1