I am currently working on taking a string containing a linear expression of the form mx + b and returning a list containing the slope (m) and vertical intercept (b) as float values.
The program works good if the string contains both m and b (i.e.; 5x+3). However, it doesn't work if the string just contains m (i.e.; 5x).
I get a
IndexError: list index out of range.
I know what the issue is, but I guess I am still lacking the skills to correct it. I wanted to reach out to see what I could do to fix it.
def getLinearParameters(mx_plus_b):
equation_values = mx_plus_b.split("+")
m = equation_values[0][:-1]
b = equation_values[1]
return [float(m), float(b)]
print(getLinearParameters("5x"))