Python Forum
Need help fixing Index Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help fixing Index Error
#1
Hi,

This is for a class project. The course is not a coding course, but we do use code to complete homework and projects (yes, it is as confusing as it sounds). Because of this, we are allowed to ask for outside help.

I am receiving an IndexError with the following code. I cannot figure out why the for loop is breaking. I've tried debugging multiple times but still cannot find which scalar variable the code is upset about. I call a function "values" within it - I do not believe that code is necessary for fixing this error.

I am trying to optimize "values" using the following arrays. Suggestions for a more efficient way of optimization would be helpful!

iteration = 40
Ltotal = np.linspace(1,20000,iteration) #m
m = np.linspace(1,40,iteration) #number of passes
ktube = np.array([400,48,160,16]) #copper,mild steel, aluminum, stainless steel
Cptube = np.array([376.8,510.8,921.1,460.5])
ptube = np.array([894,785,270,800] )
tfin = np.linspace(0.1/1000,1/1000,iteration) #m
tspace = np.linspace(1/1000,1/100,iteration) #m
ODtube = np.linspace(0.0001,1,iteration) #m
unitcost = np.array([7.50,0.34,1.87,1.98]) #$/kg

for j in range(len(ktube)):
    for i in range(iteration):
        Qtotal,effectiveness,U,Ltotal,Twaterout,Tairout,cost = values(Ltotal[i],m[i],ktube[j],Cptube[j],ptube[j],tfin[i],tspace[i],ODtube[i],unitcost[j])
        if 82000<=Qtotal<=87000:
            if j == 0:
                print('copper,Q = {}'.format(Qtotal))
            if j==1:
                print('mild steel, Q = {}'.format(Qtotal))
            if j==2:
                print('aluminum, Q = '.format(Qtotal))
            else:
                print('stainless steel, Q = {}'.format(Qtotal))
        else:
            print('option {} doesnt work Q = {}'.format(i,Qtotal))
            i+=1
Here is the error:
Error:
Qtotal,effectiveness,U,Ltotal,Twaterout,Tairout,cost = values(Ltotal[i],m[i],ktube[j],Cptube[j],ptube[j],tfin[i],tspace[i],ODtube[i],unitc2\designreport2.py", line 179, in <module>ost[j]) l[i],m[i],ktube[j],Cptube[j],ptube[j],tfin[i],tspace[i],ODtube[i],unitcost[j]) IndexError: invalid index to scalar variable.
Reply
#2
This returns the same error.
import numpy as np
x = np.array((1, 2, 3))
y = x[1]
z = y[1]
Error:
File "test.py", line 4, in <module> z = y[1] IndexError: invalid index to scalar variable.
This indicates that something that is not indexable is being indexed. In particular it means a numpy scalar is being indexed because you get a different error message if you index an int or a float.

But I don't see where this is happening in your code. I did this as a quick check.
import numpy as np

iteration = 40
Ltotal = np.linspace(1,20000,iteration) #m
m = np.linspace(1,40,iteration) #number of passes
ktube = np.array([400,48,160,16]) #copper,mild steel, aluminum, stainless steel
Cptube = np.array([376.8,510.8,921.1,460.5])
ptube = np.array([894,785,270,800] )
tfin = np.linspace(0.1/1000,1/1000,iteration) #m
tspace = np.linspace(1/1000,1/100,iteration) #m
ODtube = np.linspace(0.0001,1,iteration) #m
unitcost = np.array([7.50,0.34,1.87,1.98]) #$/kg

i = 0
j = 0
print(Ltotal[i],m[i],ktube[j],Cptube[j],ptube[j],tfin[i],tspace[i],ODtube[i],unitcost[j])
That ran with no problems, leaving this:
values(Ltotal[i],m[i],ktube[j],Cptube[j],ptube[j],tfin[i],tspace[i],ODtube[i],unitcost[j])
What is values()?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  fixing error TypeError: 'float' object is not subscriptable programmingirl 1 1,531 Jan-28-2023, 08:13 PM
Last Post: deanhystad
  how do I fix an index error? MehHz2526 1 1,239 Jan-15-2023, 08:23 PM
Last Post: Gribouillis
  could somebody tell me how I fix an index error? MehHz2526 3 1,358 Nov-29-2022, 07:47 AM
Last Post: buran
  Fixing my backup function Beginner_coder123 4 3,056 Jun-15-2019, 07:56 PM
Last Post: Beginner_coder123
  Fixing arrays output in Python WLST script pkbash 2 4,012 Feb-28-2019, 06:20 PM
Last Post: pkbash
  Need help with fixing iteration counter Kapolt 8 4,711 Oct-18-2018, 07:56 PM
Last Post: buran
  i need help in fixing my scientific calculator coding : (, im using python 3.5.5 hans77 1 4,180 Oct-17-2018, 03:26 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020