tags:

views:

193

answers:

5

Hi,

I am getting an error in the following code. The Error message is "Error: Inconsistent indentation detected!"

s=[30,40,50]
a=[5e6,6e6,7e6,8e6,8.5e6,9e6,10e6,12e6]
p=[0.0,0.002,0.004,0.006,0.008,0.01,0.015,0.05,0.1,0.15,0.2]
j=0
b=0
x=0


for j in s:
    h=s[j]
    print "here is the first loop" +h
    for b in a:
           c=a[b]                                #too much indentation
           print"here is the second loop" +c     #too much indentation
           for x in p:                           #too much indentation
                k=p[x]
             print"here is the third loop" +k

If there is any other error I will be highly obliged if anyone here could correct me.

Thanks.

/Gillani

+2  A: 

Your three lines initing your variable to zero are at a different indentation than the rest of your code. Even the code display here on stackoverflow shows that.

Also, check that you haven't mixed tabs and spaces.

Ned Batchelder
Traceback (most recent call last): File "C:/Users/esyefak/Desktop/newloops.py", line 6, in <module> h=s[j]IndexError: list index out of rangeNow here is the error which I am having??
Gillani
+6  A: 

Once you cleaned your tabs and spaces (you should have only tabs or only spaces), you'd need to fix your loops:

s = [30,40,50]
a = [5e6,6e6,7e6,8e6,8.5e6,9e6,10e6,12e6]
p = [0.0,0.002,0.004,0.006,0.008,0.01,0.015,0.05,0.1,0.15,0.2]

for j in s:        # within loop j is being 30 then 40 then 50, same true for other loops
    print "here is the first loop" + j
    for b in a:
        print"here is the second loop" + b
        for x in p:
            print"here is the third loop" + x

Otherwise you'd have IndexError.

SilentGhost
+1  A: 

I also think it's a tab/space mixing problem. Some editors, like Textmate, has the option for displaying 'invisible' characters, like tab and newline. Comes very handy when you code, especially in Python.

rubayeet
+5  A: 

SilentGhost is correct -- unlike languages like Javascript, when you write

s = [30, 40, 50]
for j in s:

Then j is not assigned 0, 1, and 2 -- it is assigned the actual values 30, 40, and 50. So there is no need to say, on another line,

h = s[j]

In fact, if you do that, the first time through the loop, it will evaluate as

h = s[30]

Which is way out of bounds for a three-element list, and you'll get an IndexError.

If you really wanted to do it the other way -- if you really needed the indexes as well as the values, you could do something like this:

s = [30, 40, 50]
for j in range(len(s)):
    h = s[j]

len(s) gives you the length of s (3, in this case), and the range function makes a new list for you, range(n) contains the integers from 0 to n-1. In this case, range(3) returns [0, 1, 2]

As SilentGhost points out in the comments, this is much more pythonic:

s = [30, 40, 50]
for (j, h) in enumerate(s):
    # do stuff here

enumerate(s) returns the three pairs (0, 30), (1, 40), and (2, 50), in that order. With that, you have the indexes into s, as well as the actual element, at the same time.

Ian Clelland
if he needs indexes and values he should be using `enumerate`.
SilentGhost
A: 

This part:

    for b in a:
           c=a[b]                                #too much indentation

the "c=a[b]" is indented 8 spaces instead of 4. It needs to be only 4 spaces (ie. one python indentation).

And Ian is right, the "for x in y" syntax is different than other languages.

list1 = [10, 20, 30]
for item in list1:
    print item

The output will be: "10, 20, 30", not "1, 2, 3".

Rocketmonkeys
"one Python indentation" is however much you want it to be. As long as the indentation increases, Python doesn't really care _how much_ it increases.
Chris Lutz
@chrisYou're right, sorry about that. While visually confusing, 'too much indentation' is syntactically fine with python.
Rocketmonkeys