You start out with the string lst = "[56 78 90 1 2 3]"
. It is split by whitespaces giving ['[56', '78', '90', '1', '2', '3]']
. Notice the brackets in the first and last element!
You then interpret each element as a float which fails on float('[56')
and float('3]')
, causing the program to enter the except
case that you carefully chose to not utilize.
Here is how you would turn an integer string into a proper list that could be iterated over, so that you could reinterpret the numbers as floats for whatever reason you would want that:
import ast
ast.literal_eval(lst.replace(" ", ", ")
Why are you passing around a bunch of integers as a string anyway? Could you show the place in your code where "[56 78 90 1 2 3]"
comes into existence?