简体   繁体   中英

Python while loop error - condition ignored by python?

Here is the code I am running:

#Import torsions.dat
f = open("torsions.txt")
next = f.readline().strip()
length = next                       #Store the first line as length

#Store the values for phi and psi in lists
phi = []
psi = []
while next != "":
    next = f.readline().strip().split(" ")
    phi.append(float(next[0]))
    psi.append(float(next[1]))

But I get this error:

enter image description here

The file torsions.txt contains this:

20

60 61

62 63

64 65


There is no space after 65. There are 4 succeeding lines (ie there's no blank line in between). THe underscores are just for clarity, they're not in the txt.

The loop stops the script due to the error, adn the part after the loop doesn't run. phi and psi get populated as required, but then the loop should stop, but it looks like it doesn't.

Could you help?

next = f.readline().strip().split(" ")

readline will return an empty string '' when done reading the file.

split will give [''] , and you can't convert '' to a float.

Also [''] != '' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM