List index out of range (works fine for list size < 25)

In summary, the user is inputting pressure (in Pa) and volume (in m3) and wants to find the pressure given the ideal gas equation with Van der Waal's correction. The user has to take a constant value for ##t## (user input), and ##v## will range from 1 to 20 in steps of 0.01. The user has written code to compute pressure from volume and temperature. When the user inputs a number greater than 20, an error is generated.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
I have a program where I am supposed to find out the pressure, given the ideal gas equation with Van der Waal's correction, $$\left(p - \frac{a}{v^2}\right)\left(v - b\right) \ = \ Rt$$I have to take a constant value for ##t## (user input), and ##v## will range from 1 to 20 in steps of 0.01. ##a = 0.002## and ##b = 1.2## are given.

I have written the following code:

Python:
import numpy as np
from sympy import Symbol, solve

class P008:
    def declarations(self):
        global R, a, b
        R = 8.314
        a = .0002
        b = 1.2

    def inp_PvsV(self):
        """
        Takes input for the P vs V file.
        """
        print("Enter the value of T: ", end="")
        temp = float(input())
        vol = np.linspace(1, 20, num=2001)
        vol = np.around(vol, decimals=2)
        self.compute_PvsV(vol, temp)

    def compute_PvsV(self, vol, temp):
        """
        Computes pressure from volume and temperature.
        :param vol: The Volume as a numpy array
        :param temp: The Temperature as a numpy array
        """
        p = Symbol("p")
        s = []
        for i in range(0, np.size(vol)):
            # Equation for p (lhs-rhs)
            eq = (p + (a / (vol[i] ** 2))) * (vol[i] - b) - R * temp
            # Now solving for p
            s1 = solve(eq, p)
            s.append(s1[0])
            print("i = ", i, " s = ", s)
        pres = np.asarray(s)
        print(pres)

obj = P008()
obj.declarations()
obj.inp_PvsV()

I found during debugging that in line no. 29, if I change np.size(vol) to anything <= 20, I get a good output:

1566561682789.png


(Couldn't give the full output screen, but the fact that it finished with exit code 0 means that there was no error.)

When I change the number to anything > 20, I get an error:

1566561804970.png


This seems more like an overflow problem. My code is unchanged, only that number in the range increases, and I get an error. But I am supposed to accomplish this task for 2001 values.

Any idea on how to do this?

I am working in Python 3.7.4, and using PyCharm IDE.
 
Technology news on Phys.org
  • #2
Note that the error is in the line that says, "s.append(s1[0])". This means that the line above which is, "s1 = solve(eq, p)" didn't return any solution. You should add a print statement above the failing line to print out s1 and the values of the equation you are trying to solve to see whay there is no solution.
 
  • Like
Likes Wrichik Basu
  • #3
phyzguy said:
Note that the error is in the line that says, "s.append(s1[0])". This means that the line above which is, "s1 = solve(eq, p)" didn't return any solution. You should add a print statement above the failing line to print out s1 and the values of the equation you are trying to solve to see whay there is no solution.
Yes, you are right. s1 is empty when i=21. I will have to see how to solve that problem.
 
  • #4
Line 17 might have something to do with it. :wink:
 
  • #5
Tom.G said:
Line 17 might have something to do with it. :wink:
Yeah, I know that. Actually there is a problem when ##v=b##, where the lhs-rhs becomes 0. I shifted that to the front by changing ##b## to 1.0, and then excluded ##v=1.0## case. With a little bit of extra effort, this can be resolved for any ##b=v## case.
 
  • #6
I would go through and print your intermediate arrays to make sure they are as you expected.

In particular, the vol array where you use linspace to populate it and then use around to limit the decimal pts which could make duplicates that might somehow get removed shortening the array. I see no evidence of that only its something I would check.
 
  • #7
jedishrfu said:
I would go through and print your intermediate arrays to make sure they are as you expected.

In particular, the vol array where you use linspace to populate it and then use around to limit the decimal pts which could make duplicates that might somehow get removed shortening the array. I see no evidence of that only its something I would check.
I did all that in the debugging phase of the program.
 
  • #8
If it doesn’t work you’re still in debugging phase.
 
  • Like
Likes FactChecker and Vanadium 50
  • #9
One company i worked for implemented an interesting metric to discover the truthfulness of the project manager based on bug tracking and how a bug was introduced into the code base.

The project phases were:
- design phase
- coding phase
- unit test
- integration test
- system test
...

So while the PM might say confidently that we are in system test ready for ship, the bug stats might say we have some design errors and we are still in coding or unit test phases.

I can’t remember the name of the system but for obvious reasons it wasn't wildly popular with folks looking for promotions.

EDIT: Its called Orthogonal Defect Classification. I think its been superseded by other techniques to get a more real-time feel for what is happening in a software project.

https://en.wikipedia.org/wiki/Orthogonal_Defect_Classification
 
Last edited:
  • Like
Likes Tom.G

Question 1: What does "List index out of range" mean?

"List index out of range" is an error message that pops up when you are trying to access an index of a list that does not exist. In other words, the index you are trying to access is larger than the size of the list.

Question 2: Why does this error occur only for lists with a size greater than 25?

This error occurs only for lists with a size greater than 25 because the index of a list starts from 0. So, for a list with a size of 25, the last index would be 24. Trying to access an index higher than 24 will result in the "List index out of range" error.

Question 3: How can I fix this error?

To fix this error, you can check the size of the list before trying to access a specific index. Make sure the index you are trying to access is within the range of the list's size. You can also use try-except blocks to handle this error gracefully.

Question 4: Is there a way to prevent this error from occurring?

Yes, there are a few ways to prevent this error. One way is to carefully check the size of your list and make sure you are not trying to access an index that is out of range. Another way is to use built-in functions like len() to get the size of the list and use that value to access specific indexes.

Question 5: Can this error occur for other types of data structures?

Yes, this error can occur for other types of data structures such as strings, tuples, and dictionaries. Just like lists, these data structures have indexes and trying to access an index that is out of range will result in the same error.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
2
Views
887
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top