2015-10-15 59 views
0

我有一個問題indetntation我的代碼:Python的壓痕問題(錯誤:預期的縮進塊)


#!/usr/bin/python 

import numpy as np 

#Hit and mess method 

A=0 #Integral left limit 
B=1 #Integral right limit 
C=1.0 #Maximum function value in interval [a,b] 
N=100000 #Number of experiments 
L=[10**x for x in range(7)] #List of powers of ten (1,10,100,...) 

def F(X): #Function to integrate 
    return np.sqrt(1-(X**2)) 

for M in L: 
    NA=0 #Number count of hits 
    for J in range(M): 
     U=np.random.rand() 
     V=np.random.rand() 
     X=A+(B-A)*U 
     if F(X) > C*V: 
      NA=NA+1   
    P=NA/float(M) #Probability 
    I=C*(B-A)*P #Integral value 
    STD=np.sqrt(P*(1-P)/M)*C*(B-A) 
    print M,NA,P,I,STD 

的錯誤是:

文件 「HIT.py」 ,第19行 U = np.random.rand() ^ IndentationError:預計有縮進塊

謝謝!

+1

呵呵......它工作絕對好! 1 1 1.0 1.0 0.0 10 6 0.6 0.6 0.154919333848 100 78 0.78 0.78 0.0414246303544 1000 790 0.79 0.79 0.0128802173895 10000 7920 0.792 0.792 0.00405876828607 100000 78426 0.78426 0.78426 0.00130075459792 百萬785119 0.785119 0.785119 0.000410739766566 – nehemiah

+1

不能再現。 – luoluo

+0

我懷疑當你複製你的代碼時,一個標籤與空間問題就消失了。你確定你只使用一種白色字符(即只有空格或只有製表符,最好是空格)? –

回答

1

您在混合製表符和空格。您的代碼使用U=np.random.rand()以上兩行中的選項卡,但是,縮進由U=np.random.rand()的空格提供。在代碼中還有很多混合空格和製表符的例子。

對於幾對線,代碼如下所示(\ t表示製表符):

for M in L: 
\tNA=0 #Number count of hits 
\tfor J in range(M): 
     U=np.random.rand() 
    \tV=np.random.rand() 
    \tX=A+(B-A)*U 

您應該只使用空格縮進的方式,如果可能的話(它可能無法保持較老的時候有可能碼)。你可以在PEP 8中閱讀關於這個和其他風格的問題。

+0

謝謝!有效! – Pep