相关疑难解决方法(0)

Python编程中的制表符与空格

当我进行Python编程时,我总是使用制表符进行缩进.但后来我在SO上遇到了一个问题,有人指出大多数Python程序员使用空格而不是制表符来最小化编辑器到编辑器的错误.

这有什么不同?还有其他原因可以使用空格而不是Python的制表符吗?或者这不是真的吗?

我应该切换我的编辑器来插入空格而不是立即插入标签或继续像以前那样继续前进吗?

python coding-style conventions indentation

339
推荐指数
19
解决办法
20万
查看次数

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万
查看次数

我得到一个IndentationError.我如何解决它?

我有一个Python脚本:

if True:
    if False:
        print('foo')
   print('bar')
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行我的脚本时,Python提出了一个IndentationError:

  File "script.py", line 4
    print('bar')
               ^
IndentationError: unindent does not match any outer indentation level
Run Code Online (Sandbox Code Playgroud)

我一直在玩我的程序,我还能够产生其他三个错误:

  • IndentationError: unexpected indent
  • IndentationError: expected an indented block
  • TabError: inconsistent use of tabs and spaces in indentation

这些错误意味着什么?我究竟做错了什么?我该如何修复我的代码?

python error-handling code-formatting exception indentation

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

Python 3中的TabError

鉴于以下解释器会话:

>>> 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 python-3.x

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

十六进制数的长度

我们怎样才能在Python语言中获得十六进制数的长度?我尝试使用此代码,但即使这显示一些错误.

i = 0
def hex_len(a):
    if a > 0x0:
        # i = 0
        i = i + 1
        a = a/16
        return i
b = 0x346
print(hex_len(b))
Run Code Online (Sandbox Code Playgroud)

这里我只使用346作为十六进制数,但我的实际数字非常大,需要手动计算.

python counting

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

在 python 中在一行中嵌套 for 循环

这是我试图提高可读性的一些代码。它可以工作,但是嵌套的 for 循环和 try/if 使乍一看有点难以理解发生了什么。

有人可以就我如何加入嵌套的 for 循环或压缩此代码给我建议吗?

matcher = None
if re.match(_RE_OBJECT, nodes.replace(LQMN, '')):
  matcher = alias
else:    
  for x in lister[0].conditions:
    for y in x.codes:
      try:
        if y.id.split(',')[1] == condition:
          matcher = x.codenames
      except IndexError:
        pass
Run Code Online (Sandbox Code Playgroud)

python for-loop nested

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