# Python Cumulative Project: Reggie_Linear_Regression

I have completed the project, but was wondering about a variant answer solution to Step2:

The correct code given for calculating the smallest error for a linear regression was:
datapoints = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)]
smallest_error = float(“inf”)
best_m = 0
best_b = 0

for m in possible_ms:
for b in possible_bs:
error = calculate_all_error(m, b, datapoints)
** if error < smallest_error:**
best_m = m
best_b = b
smallest_error = error
print(best_m, best_b, smallest_error)

My question regards the bold two lines. Why is my shorter code version not working and gives me wrong results? Basically it does the same from my view, but I am sure I am missing something.

My code:
datapoints = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)]
smallest_error = float(“inf”)
best_m = 0
best_b = 0

for m in possible_ms:
for b in possible_bs:
if (calculate_all_error(m, b, datapoints) < smallest_error):
best_m = m
best_b = b
print(best_m, best_b, smallest_error)

Thanks!!

Because in your code smallest_error doesn’t change, it’s always ‘inf’.
So, on every iteration if (calculate_all_error(m, b, datapoints) < smallest_error) is True.
You have to refresh value of smallest_error every time you found smaller error, like in example from Codeacademy.
It’s for reason, when they keep condition in variable error - to save it in smallest_error later.