I aim to print a list of every element inside a user-inputted list. The outputted list identifies symbols as strings, and numbers as floats, to basically compare elements as floats and strings. If the number in the list is already a float, the output is not printed properly.
expression_list = []
expression = "(3.1+2)*5"
for index in expression:
try:
float_index = float(index)
expression_list.append(float_index)
except ValueError:
expression_list.append(index)
print(expression_list)
I expect the output to be ['(', 3.1, '+', 2.0, ')', '*', 5.0]
instead I get ['(', 3.0, '.', 1.0, '+', 2.0, ')', '*', 5.0]