IndexError: invalid index to scalar variable for double and if 'for' statement
<p>My main aim is to print out the values of one variable which is in a double <code>for loop</code>  and under an  <code>if-statement</code>but however I tend to get an error <code>IndexError: invalid index to scalar variable.</code> Here is my codes</p>

<pre><code>import numpy as np

nR = 133
nP  = 255
Pmin = 0.09
Pmax = 20.0
dTime = 0.005

g = np.zeros((nR+1, nP+1), dtype=np.float64)
E01 = np.zeros(nR+1)
LnP = np.zeros(nP+1)


n = nR - 1
l = nP - 1

LnPmax = np.log(Pmax)
LnPmin = np.log(Pmin)
dLnP = (LnPmax - LnPmin)/(l-1)
odt = 1/dTime

for i in range(1, n):
    T1  = E01[i]*dTime
    for k in range(1, l):
        LnPa = T1 + LnP[k]
        ka = int((LnPa - LnPmin)/dLnP + 1)
        if LnPa &lt;= LnPmin:
            ha = 0.0
        elif LnPa &gt;= LnPmax:
            ha = 0.0
        else:
            ha = g[i][ka] + (g[i][ka+1] - g[i][ka])*(LnPa - LnPa[ka])/dLnP
            print ha
</code></pre>

<p>whenever i try to <code>print ha</code> I get an error <code>IndexError: invalid index to scalar variable</code>, I have looked up to other solution on <code>stackoverflow</code> and <code>google</code> but it was to no avail perhaps anyone could help me, I will appreciate <code>:)</code></p>

<p>In your next-to-last line, you have the sub-expression</p>

<pre><code>LnPa - LnPa[ka]
</code></pre>

<p>That first reference to <code>LnPa</code> is fine, since that is a float variable, but the second is not. You are trying to treat that float variable as a list or some-such with <code>LnPa[ka]</code>, by indexing it with <code>ka</code>. I do not know what you intend (you really need to add comments and make your variable names more descriptive), so I can't recommend a particular correction. But somehow get rid of <code>LnPa[ka]</code>.</p>

<p>Also, the last line <code>print ha</code> does not work in python 3.x. If you replace it with</p>

<pre><code>print(ha)
</code></pre>

<p>it will work in any version of python.</p>

python numpy if-statement for-loop