<aside> 💡
Driver Lab
import random
# we are going to find the value of x for which ax + b = 0
# Randomly choose values for a and b
a = random.randint(-5, 5)
b = random.randint(-5, 5)
print(f"Checking values for the equation: {a}x + {b} = 0\n")
# Iterate through all x values from -15 to 15
for x in range(-15, 16):
result = a * x + b
print(f"x = {x}: {a}({x}) + {b} = {result} {'✅ Equals 0' if result == 0 else ''}")
If you want to make sure you can always find an answer, even if it is a fraction, try modifying the code to increase by one-tenths instead of by one.
import random
def frange(start, stop, step):
"""Generate numbers from start to stop, increasing by step."""
while start <= stop:
yield round(start, 10) # Round to avoid floating-point precision issues
start += step
# Randomly choose values for a and b
a = random.randint(-15, 15)
b = random.randint(-15, 15)
print(f"Checking values for the equation: {a}x + {b} = 0\n")
# Iterate through all x values from -15 to 15 in steps of 0.1
for x in frange(-15, 15, 0.1):
result = a * x + b
print(f"x = {x:.1f}: {a}({x:.1f}) + {b} = {result:.2f} {'Equals 0' if abs(result) < 1e-6 else ''}")
What do you think would happen if we decided to increase by one-hundredths instead, or one-thousandths?
</aside>