简体   繁体   English

制表符和空格的使用不一致

[英]Inconsistent use of tabs and spaces

I'm getting the error "inconsistent use of tabs and spaces in indentation" but as far as I can tell everything is as it should be.我收到错误“在缩进中使用制表符和空格不一致”,但据我所知,一切都应该如此。 Specifically it refers to the for item in poly(1:): and it has an up carrot below the last colon.具体来说,它指的是for item in poly(1:):for item in poly(1:):并且在最后一个冒号下方有一个向上的胡萝卜。 I'm using Notepad++ editor and Python 3.4.我正在使用 Notepad++ 编辑器和 Python 3.4。 Any thoughts?有什么想法吗?

def compute_deriv(poly):
    new_poly = ()
    for item in poly(1:):
        new_poly.append(poly.index(item)*item)
    return new_poly

print(compute_deriv(-13.89,0.0,17.5,3.0,1.0))

You are not creating a list at all and are slicing incorrectly:您根本没有创建列表并且切片不正确:

new_poly = [] # now it's a list
for item in poly[1:]: # poly[1:] not poly(1:)

Your syntax is completely invalid, tabs and spaces are not the cause of that.您的语法完全无效,制表符和空格不是造成这种情况的原因。 You also cannot append to a tuple, you append to a list.你也不能附加到一个元组,你附加到一个列表。

I would also use enumerate to get the indexes unless you only want the first index for repeated elements:我也会使用 enumerate 来获取索引,除非你只想要重复元素的第一个索引:

def compute_deriv(poly):
    new_poly = []
    for ind , item in enumerate(poly[1:],1):
        new_poly.append(ind*item)
    return new_poly

If you want a tuple then you should know tuples have no append and they are immutable so you would have to create a new tuple every iteration or simply use a list and return tuple(new_poly) .如果你想要一个元组,那么你应该知道元组没有附加并且它们是不可变的,所以你必须在每次迭代时创建一个新的元组或者简单地使用一个列表并返回tuple(new_poly)

You also pass no sliceable object when you call the function, instead you pass 5 args to a function that takes 1.您在调用函数时也不传递可切片对象,而是将 5 个参数传递给采用 1 的函数。

If you really want a tuple just use a gen exp and call tuple in it:如果你真的想要一个元组,只需使用 gen exp 并在其中调用元组:

def compute_deriv(poly):
    return tuple(ind*item for ind, item in enumerate(poly[1:]))

Call it passing a tuple of items:调用它传递一组项目:

 print(compute_deriv((-13.89,0.0,17.5,3.0,1.0)))

Output:输出:

(0.0, 17.5, 6.0, 3.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM