could not convert string to float (python)

后端 未结 2 870
悲哀的现实
悲哀的现实 2021-01-29 14:54

I am trying to find the sum of all numbers in a list but every time I try I get an error that it cannot convert the string to float. Here is what I have so far.

         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-29 15:20

    You need none of your code above. The while loop is unnecessary and it looks like its just exiting the loop in one iteration i.e. its not used correctly. If you're simply summing all the values in the list:

    sum([float(i) for i in elec_used])
    

    If this produces errors, please post your elec_used list. It probably contains string values or blank spaces.

提交回复
热议问题