相关疑难解决方法(0)

令人困惑的错误

在下面我得到错误

IndentationError: unindent does not match any outer indentation level (hisrel_split.py, line 25)
Run Code Online (Sandbox Code Playgroud)

elif条线.我检查了所有的缩进,甚至重新输入了大部分内容.要么我错过了一些明显的东西,要么有一些我不知道的微妙规则.有任何想法吗

from numpy import *
from pylab import *
import sys

ifp = open(sys.argv[1],"r").readlines()
data_1 = []
data_2 = []
data = []

last = int(ifp[-1].split()[0])
set_1 = range(int(round(last)/2))
set_2 = range(int(round(last)/2),(last+1))

for i in ifp:
    d = i.split()
    try:
        data.append(eval(d[2]))
    except:
        continue
    if eval(d[0]) in set_1 and eval(d[1]) in set_1:
        try:
            data_1.append(eval(d[2]))
        except:
            continue
    elif eval(d[0]) in set_2 and eval(d[1]) in set_2:
        print "yes"
Run Code Online (Sandbox Code Playgroud)

python python-2.7

2
推荐指数
1
解决办法
112
查看次数

具有嵌套多个条件的if语句

我试图在Python中的if语句中添加多个条件,如下所示:

if (h9 == h1 or h9 == h2 or h9 == h3 or h9 == h4 or h9 == h5 or h9 == h6 or h9 == h7 or h9 == h8) and (h10 == h1 or h10 == h2 or h10 == h3 or h10 == h4 or h10 == h5 or h10 == h6 or h10 == h7 or h10 == h8) :
     do sth.
Run Code Online (Sandbox Code Playgroud)

基本上它是一个OR条件都h9h10在同一时间.
但是,这不起作用,并给出如下错误:

IndentationError: unindent does not match any …

python if-statement

2
推荐指数
1
解决办法
7224
查看次数

Python预计会出现缩进块错误

我刚开始学习python.在我第一次遇到python时,我遇到了问题.我正在学习定义一个函数.在一个简单的命令中,我得到一个预期的缩进块错误.请指教.我正在使用这种语法

>>>def hello():
...print "Hello"
Run Code Online (Sandbox Code Playgroud)

但是当我在"Hello"之后按下回车键时,我得到预期的缩进块错误实际上我必须将此函数定义如下

>>> def hello():
print "Hello"
print "Computers are Fun"
Run Code Online (Sandbox Code Playgroud)

但我无处可去.请帮助我做错了什么

python

2
推荐指数
1
解决办法
1万
查看次数

功能中的缩进不能按预期工作

我找到了一些对我没有意义的东西.我希望有人可以解释一下.

def test(word):
    total = 0
    for l in word:
        print l

test('python')
Run Code Online (Sandbox Code Playgroud)

结果是'python'(每个字母出现在它自己的行上).

def test(word):
    total = 0
    for l in word:
        print l
    return total

test('python')
Run Code Online (Sandbox Code Playgroud)

结果只是'p'.

为什么添加return声明有这种效果?两个代码块不应该做同样的事情吗?这是否意味着它只通过for循环一次然后执行return语句?

python python-2.7

2
推荐指数
2
解决办法
382
查看次数

我可以故意写"错误缩进"的Python代码吗?

在自由格式语言中,有时我使用缩进来表示我的语句中的一些隐式结构.在下面的例子中,我只是做一个序列,prints但缩进表示第一个和第四个打印语句将两个中间"包围".

print("<div>")
  print("hello")
  print("world")
print("</div>")
Run Code Online (Sandbox Code Playgroud)

有没有办法在不触发的情况下在Python中执行类似的操作IndentationError: unexpected indent

到目前为止,我能想到的最好的方法是使用空白if语句来引入新的缩进级别.

print("<div>")
if True:
  print("hello")
  print("world")
print("</div>")
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
127
查看次数

为什么我得到python manage.py syncdb的错误(IndentationError:意外缩进)

from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.CharField(max_length=1000)
    created = models.DateField()
    modified = models.DateField()
Run Code Online (Sandbox Code Playgroud)

python manage.py syncdb出错:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/validation.py", line 35, in get_validation_errors
    for (app_name, …
Run Code Online (Sandbox Code Playgroud)

python django model

1
推荐指数
1
解决办法
1191
查看次数

Python IndentationError:Xcode 上“预期有缩进块”

我在 xcode 和 vi 上遇到了这个错误。Python 表示行类 LeastModel 有一个 IndentationError: Expected an indented block。我在 Xcode 上检查了我的首选项,以使用 4 个空格作为制表符,并且在我一直使用制表符的任何地方。请帮我!

def make_model(data,model):

class LeastModel():
    """
    linear system solved using linear least squares
    This class serves as an example that fulfills the model interface needed by the ransa function.
    """
    def __init__(self,input_columns,output_columns):
        self.input_columns = input_columns
        self.output_columns = output_columns
        #self.debug = debug
Run Code Online (Sandbox Code Playgroud)

python xcode indentation

1
推荐指数
1
解决办法
2万
查看次数

try上的缩进错误,除了python中的else块

对于我在这里问的一个问题,我很难解决错误异常.我已经整理出从另一个文件中读取文件列表,但是如果引用的其中一个文件不存在,我就会挣扎.我希望能够识别错误,发送电子邮件,然后创建该文件供以后使用.但是,我正在为"我正在尝试的"尝试,除了,"块"收到缩进错误.它看起来应该可以正常工作,但我无法让它运行!尔加!救命!!!

日志文本文件包含以下内容:

//server-1/data/instances/devapp/log/audit.log

//server-1/data/instances/devapp/log/bizman.db.log

//server-1/data/instances/devapp/log/foo.txt# 服务器上不存在此文件

我的代码如下.我认为最好将它全部发布而不是一个片段,以防它在程序中更早出现它的东西!

import os, datetime, smtplib
today = datetime.datetime.now().strftime('%Y-%m-%d')
time_a = datetime.datetime.now().strftime('%Y%m%d %H-%M-%S')
checkdir = '/cygdrive/c/bob/python_'+ datetime.datetime.now().strftime('%Y-%m-%d')+'_test'
logdir = '/cygdrive/c/bob/logs.txt'
errors = '/cygdrive/c/bob/errors.txt'

#email stuff
sender = 'errors@company.com'
receivers = 'bob@company.com'
message_1 = """From: errors <errors@company.com>
To: Bob <bob@company.com>
Subject: Log file not found on server

A log file has not been found for the automated check. 
The file has now been created.
""" 
#end of email stuff


try:
                os.chdir (checkdir) # Try …
Run Code Online (Sandbox Code Playgroud)

python indentation ioerror

1
推荐指数
1
解决办法
8405
查看次数

如何处理IndentationError:unindent与任何外部缩进级别都不匹配

我到处搜索,我无法在任何地方找到答案,我的代码遇到了问题,我正在尝试用peewee做一个简单的清单.我所拥有的就是这个错误.

  File "inventario.py", line 38
if selection =='1':
                  ^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)

我已经读过与空格混合的标签可以实现这一点,已经检查过,甚至使用SublimeText工具必须用空格替换制表符.

from peewee import *

db = SqliteDatabase('stockpy.db') #creates db

class Product(Model): #data to create product table
    id_prod = IntegerField()
    name = CharField()
    price = IntegerField()
    stock = IntegerField()

    class Meta:
        database = db #tells which db to use 

Product.create_table() #creates table

def newProd(id_prod, name, price, stock):
     Product.create(id_prod =  id_prod, name = name, price = price, stock = stock) #adds new product …
Run Code Online (Sandbox Code Playgroud)

python database peewee

1
推荐指数
1
解决办法
4975
查看次数

循环时Python缩进错误

while x <= 9:
    result = usertype()
    if result == 'Correct':
Run Code Online (Sandbox Code Playgroud)

我在"result = usertype()"的结果"t"上收到缩进错误.有人能解释一下吗?

*编辑我试过重写它,我检查确保所有的缩进确实是缩进而不是空格.现在变得非常沮丧和困惑.我认为它可能是导致问题的前一行中的某些内容.

*编辑2从命令提示符复制错误,因为IDLE对我不起作用:

  File "<stdin>", line 5
    result = usertype()
         ^
IndentationError: expected an indented block
>>>         if result == 'Correct':
  File "<stdin>", line 1
    if result == 'Correct':
    ^
IndentationError: unexpected indent
>>>             x = x + 1
  File "<stdin>", line 1
    x = x + 1
    ^
IndentationError: unexpected indent
>>>             y = y + 5
  File "<stdin>", line 1
    y = y + 5 …
Run Code Online (Sandbox Code Playgroud)

python syntax indentation syntax-error while-loop

1
推荐指数
1
解决办法
4897
查看次数