相关疑难解决方法(0)

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

当我编译下面的Python代码时,我得到了

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


import sys

def Factorial(n): # Return factorial
    result = 1
    for i in range (1,n):
        result = result * i
    print "factorial is ",result
    return result
Run Code Online (Sandbox Code Playgroud)

为什么?

python indentation

525
推荐指数
16
解决办法
136万
查看次数

如何使用pass语句?

我正在学习Python,我已经达到了有关该pass声明的部分.我正在使用的指南将其定义Null为通常用作占位符的语句.

我仍然不完全明白这意味着什么.有人能告诉我一个简单/基本的情况,其中pass将使用该声明以及为什么需要它?

python

359
推荐指数
8
解决办法
26万
查看次数

关于Python while语句的Else子句

我注意到以下代码在Python中是合法的.我的问题是为什么?有具体原因吗?

n = 5
while n != 0:
    print n
    n -= 1
else:
    print "what the..."
Run Code Online (Sandbox Code Playgroud)

python syntax if-statement while-loop

293
推荐指数
7
解决办法
17万
查看次数

如何修复Python缩进

我有一些Python代码具有不一致的缩进.有很多标签和空格的混合使事情变得更糟,甚至空间压痕也不会被保留.

代码按预期工作,但很难维护.

如何在不破坏代码的情况下修复缩进(如HTML Tidy,但对于Python)?

python

253
推荐指数
9
解决办法
24万
查看次数

"在缩进中使用制表符和空格不一致"

我正在尝试在Python 3.2中创建一个应用程序,并且我一直使用制表符进行缩进,但即使编辑器将其中的一些更改为空格,然后在我尝试运行时打印出"缩进时使用制表符和空格的不一致"该程序.

如何将空格更改为标签?这让我疯狂.(我是编程的初学者).如果我可以获得关于我的代码的一些整体提示,我会很高兴,如果我犯了很多错误,我会很高兴听到.

import random

attraktioner = ["frittfall","bergodalbana","spökhuset"]


class Nojesfalt:
    def __init__(self, attraktion):
        self.val = attraktion
        self.langd = 0
        self.alder = 0


#längdgräns för fritt fall
    def langdgrans(self):
        print("")
        self.langd = int(input("Hur lång är du i cm? "))
        if self.langd < 140:
            print("tyvärr, du är för kort, prova något annat")
            return 0
        elif self.langd >= 140:
            print("håll dig hatten, nu åker vi!")
            print(" ")
            return 1

#åldersgräns för spökhuset
    def aldersgrans(self):
        print("")
        self.alder = int(input("Hur gammal är du? "))
        if …
Run Code Online (Sandbox Code Playgroud)

python

175
推荐指数
12
解决办法
65万
查看次数

106
推荐指数
9
解决办法
66万
查看次数

Python AttributeError:对象没有属性

我有一个MyThread课程.我有一个方法样本.我试图从具有相同的对象上下文运行它.请看一下代码:

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter, redisOpsObj):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.redisOpsObj = redisOpsObj

    def stop(self):
        self.kill_received = True

    def sample(self):
        print "Hello"

    def run(self):
        time.sleep(0.1)
        print "\n Starting " + self.name
        self.sample()
Run Code Online (Sandbox Code Playgroud)

看起来很简单不是吗.但是当我运行它时,我得到了这个错误

AttributeError: 'myThread' object has no attribute 'sample'现在我有那种方法,就在那里.那有什么不对?请帮忙

编辑:这是堆栈跟踪

Starting Thread-0

Starting Thread-1
Exception in thread Thread-0:
Traceback (most recent call last):
File "/usr/lib/python2.6/threading.py", line 525, in __bootstrap_inner
self.run()
File "./redisQueueProcessor.py", line 51, in run
self.sample()
AttributeError: …
Run Code Online (Sandbox Code Playgroud)

python

61
推荐指数
4
解决办法
29万
查看次数

为什么我会收到"IndentationError:期望缩进块"?

if len(trashed_files) == 0 :
    print "No files trashed from current dir ('%s')" % os.path.realpath(os.curdir)
else :
    index=raw_input("What file to restore [0..%d]: " % (len(trashed_files)-1))
    if index == "*" :
        for tfile in trashed_files :
            try:
                tfile.restore()
            except IOError, e:
                import sys
                print >> sys.stderr, str(e)
                sys.exit(1)
    elif index == "" :
        print "Exiting"
    else :
        index = int(index)
        try:
            trashed_files[index].restore()
        except IOError, e:
            import sys
            print >> sys.stderr, str(e)
            sys.exit(1)
Run Code Online (Sandbox Code Playgroud)

我正进入(状态:

        elif index == "" :
        ^
    IndentationError: expected …
Run Code Online (Sandbox Code Playgroud)

python

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

Notepad ++缩进搞砸了

我用Python编写代码,我非常喜欢Notepad ++.但是,当我使用tab缩进时,在Notepad ++中似乎很好,但是当我运行程序时,我得到一个缩进错误,当我在Emacs中检查我的代码时,我发现Notepad ++实际上添加的选项卡空间比它显示在屏幕上.怎么了?

python notepad++ indentation

44
推荐指数
3
解决办法
5万
查看次数

Python def函数:如何指定函数的结尾?

我只是学习python并且在函数的"def"结束时感到困惑?

我看到代码示例如下:

def myfunc(a=4,b=6):
    sum = a + b
    return sum

myfunc()
Run Code Online (Sandbox Code Playgroud)

我知道它不会因为返回而结束(因为我已经看过if语句......如果FOO比返回BAR,否则返回FOOBAR).Python如何知道这不是一个自称的递归函数?当函数运行时,它只是继续通过程序直到它找到返回?这会导致一些有趣的错误.

谢谢

python syntax

42
推荐指数
4
解决办法
12万
查看次数