Python上的意外缩进?

Python上的意外缩进?,python,indentation,Python,Indentation,昨天,我在尝试一些MOOC,它将python包含在一些编程任务中。我试图解决一个简单的问题,但我不明白为什么我仍然会遇到这样的错误: In [32]: import A1Part3 File "A1Part3.py", line 26 t = np.arrange(x.size/N) ^ IndentationError: unexpected indent 我不明白为什么,我在读Python缩进,并且我知道如果上面的块中有一些语句缩进,它应该考虑函数的一部分。 我

昨天,我在尝试一些MOOC,它将python包含在一些编程任务中。我试图解决一个简单的问题,但我不明白为什么我仍然会遇到这样的错误:

    In [32]: import A1Part3
  File "A1Part3.py", line 26
    t = np.arrange(x.size/N)
    ^
IndentationError: unexpected indent

我不明白为什么,我在读Python缩进,并且我知道如果上面的块中有一些语句缩进,它应该考虑函数的一部分。 我不知道我做错了什么或者我误解了什么

    """
A1-Part-3: Python array indexing

Write a function that given a numpy array x, returns every Nth element in x, starting from the 
first element.  

The input arguments to this function are a numpy array x and a positive integer N such that N < number of 
elements in x. The output of this function should be a numpy array.

If you run your code with x = np.arange(10) and N = 2, the function should return the following output: 
[0, 2, 4, 6, 8].
"""

import numpy as np


def hopSamples(x,N):
    """
    Inputs:
        x: input numpy array
        N: a positive integer, (indicating hop size)
    Output:
        A numpy array containing every Nth element in x, starting from the first element in x.
    """
    ## Your code here   
    t = np.arrange(x.size/N)
    cont = 0
    i = 0
    while cont<x.size :
          cont+=N
          t[i]=x[cont]
          i=i+1
    return t
“”“
A1-第3部分:Python数组索引
编写一个函数,给定numpy数组x,从
第一个要素。
这个函数的输入参数是一个numpy数组x和一个正整数N,这样N<
元素。此函数的输出应为numpy数组。
如果使用x=np.arange(10)和N=2运行代码,函数应返回以下输出:
[0, 2, 4, 6, 8].
"""
将numpy作为np导入
def样本(x,N):
"""
投入:
x:输入numpy数组
N:正整数(表示跃点大小)
输出:
包含x中每n个元素的numpy数组,从x中的第一个元素开始。
"""
##你的代码在这里
t=np.排列(x.尺寸/N)
cont=0
i=0
而cont在该行上有一个制表符,Python将制表符扩展到8个空格。但前一行仅使用空格,因此其缩进为4个字符:

>>> '''\
...     ## Your code here       
...     t = np.arrange(x.size/N)
... '''
'    ## Your code here\t\n\tt = np.arrange(x.size/N)\n'
>>> #                 ^^  ^^
\t
转义序列表示此处的选项卡。由于制表符扩展为8个空格,因此该行实际上比前面的行缩进得更远

在新的Python代码中不应使用制表符。只使用空格。将编辑器配置为使用空格进行缩进(即使按tab键,大多数编辑器也会使用空格)


通过
python-tt scriptname.py运行您的代码,让python告诉您任何其他混合制表符和空格的地方,并将它们全部更正。

在我的计算机中,除了第一行的缩进之外,您的代码工作正常: 但即使您的代码出现任何缩进错误,我建议您将tab替换为4个空格