所以我正在读取一个文件并将其存储在变量中,当我到达这一行时:
valP=float(listEleVP)
它返回这个:
ValueError: could not convert string to float: 'r'
这是整个代码:
def main():
inFile=open('3weightcalc.in','r')
i=0
for line in inFile.readlines():
a=line.split()
for items in a:
listEleVP=items[1]
listEleVS=items[3]
valP=float(listEleVP)
valS=float(listEleVS)
FinVal=float(valP*valS)
txtA=items [0]
txtB=items [2]
print("On ",txtB,txtA,"would weigh ",FinVal,"pounds.")
inFile.close()
main()
这是输入(.in)文件数据:
Fred 179.0 Luna 0.1654
Layla 131 Mars 0.376
Pat 145.2 Neptune 1.14
END 0 0 0