Reputation: 492
Yesterday, I was trying some MOOC that involves Python in some of the programming tasks. I was trying to solve an easy problem, but i cant understand why I'm still getting an error like this:
In [32]: import A1Part3
File "A1Part3.py", line 26
t = np.arrange(x.size/N)
^
IndentationError: unexpected indent
I do not understand why, I was reading about python indentation, and as far I know if some statement is indented inside the block above it should consider part of that function. I don't know what I'm doing wrong or what I am misunderstanding.
"""
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
Upvotes: 0
Views: 2055
Reputation: 799
In my computer, your code works fine, except the indentation of the first row.
But even if your code get any indentation error, I suggest you to replace tabs by four spaces.
Upvotes: 0
Reputation: 1124518
You have a tab character on that line, and Python expands tabs to 8 spaces. The preceding line however only uses spaces, so it is rated at 4 characters indent:
>>> '''\
... ## Your code here
... t = np.arrange(x.size/N)
... '''
' ## Your code here\t\n\tt = np.arrange(x.size/N)\n'
>>> # ^^ ^^
The \t
escape sequences represent tabs there. Because a tab is expanded to 8 spaces, you have effectively indented that line further than the preceding lines.
You should not use tabs in new Python code. Use spaces only. Configure your editor to use spaces for indentation (most editors will then use spaces even if you pressed the tab key).
Run your code through python -tt scriptname.py
to have Python tell you about any other places you are mixing tabs and spaces, and correct them all.
Upvotes: 1