English 中文(简体)
未预料到的在沙捞? [复制]
原标题:Unexpected indent on Python? [duplicate]

昨天,我尝试了一些网站MOOC,其中涉及一些方案拟定任务。 我正试图解决一个容易的问题,但我无法理解为什么我仍会遇到这样的错误:

    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
最佳回答

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 = np.arrange(x.size/N)
 
>>> #                 ^^  ^^

/code>. 越航顺序是其中的表格。 由于一个表格被扩大至8个空间,你实际上比前线更宽松了这条线。

你们不应在新的法宝中使用制片。 仅使用空间。 没收你的编辑使用场所进行登革热(多数编辑将使用空间,即使你用上表钥匙)。

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.

问题回答

在我的电脑中,除第一行的登革热外,你的代码是行的。

But even if your code get any indentation error, I suggest you to replace tabs by four spaces.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签