Python read file into list - edit <ul> <li><p>edit - It seems that I have made an error with the calculation of number of parts tested:</p> <p><code>lines = len(file.readlines())</code> <code>N = lines - 2</code></p></li> </ul> <p>It did work when i tested it in a separate script though...</p> <p>Beginner here in need of help. I have a problem with my python script which I haven't been able to work out by myself. The script is supposed to read floating point from a text file and do some calculations. I can't get the numbers into the list <code>R[]</code>.</p> <p>Below is a sample of the text file where the first line (<code>s[0]</code>) is the nominal value, line two (<code>s[1]</code>) is the tolerance and the following lines are some resistors.</p> <hr> <p>3300.0</p> <p>10.0</p> <p>3132.0</p> <p>3348.5</p> <p>3557.3</p> <p>3467.4</p> <p>3212.0</p> <p>3084.6</p> <p>3324.0</p> <p>I have the following code:</p> <pre><code>R = [] Ntoolow = Nlow = Nhigh = Ntoohigh = 0.0 lines = 0 def find_mean(q): tot = 0.0 c = len(q) for x in range (c): tot += q[x] return tot/c def find_median(q): c = len(q) if c%2: return float(q[int(c/2)]) else: c /= 2 return (q[int(c)]+q[int(c-1)])/2.0 file_name = input("Please enter the file name: ") file = open(file_name, "r") s = file.readlines() Rnom = float(s[0]) Tol = float(s[1]) keepgoing = True while keepgoing: s = file.readline() if s == "": keepgoing = False else: R.append(float(s)) lines = len(file.readlines()) N = lines - 2 R.sort() Rmin = R[0] Rmax = R[N-1] Rlowerlimit = Rnom - Tol Rupperlimit = Rnom + Tol for rn in R: if rn &lt; Rlowerlimit: Ntoolow += 1 elif rn &lt; Rnom: Nlow += 1 elif rn &lt;= Rupperlimit: Nhigh += 1 else: Ntoohigh += 1 Ptoolow = 100.0 * Ntoolow / N Plow = 100.0 * Nlow / N Phigh = 100.0 * Nhigh / N Ptoohigh = 100.0 * Ntoohigh / N Rmean = find_mean(R) Rmedian = find_median(R) print("Total number of parts tested: " + str(N)) print("The largest resistor is: " + str(Rmax) + " and the smallest is: " + str(Rmin)) print("The mean is: " + str(Rmean) + " and the median is: " + str(Rmedian)) print("The percentage of resistors that have too low tolerance is: " + str(Ptoolow) + "%") print("The percentage of resistors that have low tolerance is: " + str(Plow) + "%") print("The percentage of resistors that have high tolerance is: " + str(Phigh) + "%") print("The percentage of resistors that have too high tolerance is: " + str(Ptoohigh) + "%") file.close() </code></pre>