0

I have an indetntation issue with my code:


#!/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

The error is:

File "HIT.py", line 19 U=np.random.rand() ^ IndentationError: expected an indented block

Thanks!

Pep
  • 19
  • 4
  • 1
    Huh... It works absolutely fine !! 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 1000000 785119 0.785119 0.785119 0.000410739766566 – nehem Oct 15 '15 at 10:07
  • I suspect a tab vs. space problem that went away when you copied your code. Do you make sure that you only use one type of white character (i.e. only spaces or only tabs, preferably spaces) ? – Quentin Roy Oct 15 '15 at 10:11
  • Space vs. tab issue in your editor? – John Coleman Oct 15 '15 at 10:11
  • In doubt, Just copy paste from this SO question, It'll solve your problem – nehem Oct 15 '15 at 10:11
  • Possible duplicate of [What to do with "Unexpected indent" in python?](http://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python) – Gall Oct 15 '15 at 10:17

1 Answers1

1

You are mixing tabs and spaces. Your code uses tabs in the 2 lines above U=np.random.rand(), however, indentation is provided by spaces for U=np.random.rand(). And there are many other examples of mixed spaces and tabs in your code.

For a few of the lines, the code looks like this (\t represents a tab):

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

You should only use spaces for indentation, if possible (it may not be possible when maintaining older code). You can read about this and other style issues in PEP 8.

mhawke
  • 84,695
  • 9
  • 117
  • 138