finding a float form a string without regex python
Question:
I am trying to take an input from a “raw_input” function and make it into 3 floats and then sum them up.
user_input = "1.23+2.25+3.25"
is it possible to take the 3 numbers and add them to a list of floats that look like this or something similar?
float_lst = [1.23,2.25,3.25]
Answers:
If I only go by your requirement, not the list, you can eval. Trivial code example below
a = raw_input()
print eval(a)
Yes.
float_lst = [float(i) for i in user_input.split("+")]
You can use the split function and then cast the elements to float.
user_input = "1.23+2.25+3.25"
lst = user_input.split("+")
lst = [float(i) for i in lst]
Now you have a list of float so you can do
result = sum(lst)
And you will have the result
I am trying to take an input from a “raw_input” function and make it into 3 floats and then sum them up.
user_input = "1.23+2.25+3.25"
is it possible to take the 3 numbers and add them to a list of floats that look like this or something similar?
float_lst = [1.23,2.25,3.25]
If I only go by your requirement, not the list, you can eval. Trivial code example below
a = raw_input()
print eval(a)
Yes.
float_lst = [float(i) for i in user_input.split("+")]
You can use the split function and then cast the elements to float.
user_input = "1.23+2.25+3.25"
lst = user_input.split("+")
lst = [float(i) for i in lst]
Now you have a list of float so you can do
result = sum(lst)
And you will have the result