"""
Simultaneous Equations Solver and Practice Tool
-----------------------------------------------
Features:
1. Solve 2-variable linear equations
2. Solve 3-variable linear equations
3. Detect no unique solution
4. Random quiz mode
5. Step-by-step style output
Form:
For 2 variables:
a1x + b1y = c1
a2x + b2y = c2
For 3 variables:
a1x + b1y + c1z = d1
a2x + b2y + c2z = d2
a3x + b3y + c3z = d3
"""
import random
# -----------------------------------
# Utility functions
# -----------------------------------
def line():
print("-" * 60)
def safe_int_input(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a valid integer.")
def format_equation_2(a, b, c):
eq = ""
# a*x
if a == 1:
eq += "x"
elif a == -1:
eq += "-x"
else:
eq += f"{a}x"
# b*y
if b > 0:
if b == 1:
eq += " + y"
else:
eq += f" + {b}y"
elif b < 0:
if b == -1:
eq += " - y"
else:
eq += f" - {abs(b)}y"
eq += f" = {c}"
return eq
def format_equation_3(a, b, c, d):
eq = ""
#x
if a == 1:
eq += "x"
elif a == -1:
eq += "-x"
else:
eq += f"{a}x"
#y
if b > 0:
if b == 1:
eq += " + y"
else:
eq += f" + {b}y"
elif b < 0:
if b == -1:
eq += " - y"
else:
eq += f" - {abs(b)}y"
#z
if c > 0:
if c == 1:
eq += " + z"
else:
eq += f" + {c}z"
elif c < 0:
if c == -1:
eq += " - z"
else:
eq += f" - {abs(c)}z"
eq += f" = {d}"
return eq
# -----------------------------------
# 2-variable solver using elimination / determinant
# -----------------------------------
def solve_2_variable():
print("\nEnter coefficients for:")
print("a1x + b1y = c1")
print("a2x + b2y = c2\n")
a1 = safe_int_input("a1: ")
b1 = safe_int_input("b1: ")
c1 = safe_int_input("c1: ")
a2 = safe_int_input("a2: ")
b2 = safe_int_input("b2: ")
c2 = safe_int_input("c2: ")
line()
print("Your system:")
print("1)", format_equation_2(a1, b1, c1))
print("2)", format_equation_2(a2, b2, c2))
line()
determinant = a1 * b2 - a2 * b1
print(f"Determinant = a1*b2 - a2*b1 = ({a1}*{b2}) - ({a2}*{b1}) = {determinant}")
if determinant == 0:
print("\nNo unique solution exists.")
print("The lines may be parallel or the same line.")
return
x = (c1 * b2 - c2 * b1) / determinant
y = (a1 * c2 - a2 * c1) / determinant
print(f"\nx = (c1*b2 - c2*b1) / determinant")
print(f"x = ({c1}*{b2} - {c2}*{b1}) / {determinant}")
print(f"x = {x}")
print(f"\ny = (a1*c2 - a2*c1) / determinant")
print(f"y = ({a1}*{c2} - {a2}*{c1}) / {determinant}")
print(f"y = {y}")
line()
print(f"Solution: x = {x}, y = {y}")
line()
# -----------------------------------
# 3-variable solver using Cramer's Rule
# -----------------------------------
def det3(m):
return (
m[0][0] * (m[1][1]*m[2][2] - m[1][2]*m[2][1])
- m[0][1] * (m[1][0]*m[2][2] - m[1][2]*m[2][0])
+ m[0][2] * (m[1][0]*m[2][1] - m[1][1]*m[2][0])
)
def replace_column(matrix, col_index, new_col):
copied = [row[:] for row in matrix]
for i in range(3):
copied[i][col_index] = new_col[i]
return copied
def solve_3_variable():
print("\nEnter coefficients for:")
print("a1x + b1y + c1z = d1")
print("a2x + b2y + c2z = d2")
print("a3x + b3y + c3z = d3\n")
a1 = safe_int_input("a1: ")
b1 = safe_int_input("b1: ")
c1 = safe_int_input("c1: ")
d1 = safe_int_input("d1: ")
a2 = safe_int_input("a2: ")
b2 = safe_int_input("b2: ")
c2 = safe_int_input("c2: ")
d2 = safe_int_input("d2: ")
a3 = safe_int_input("a3: ")
b3 = safe_int_input("b3: ")
c3 = safe_int_input("c3: ")
d3 = safe_int_input("d3: ")
line()
print("Your system:")
print("1)", format_equation_3(a1, b1, c1, d1))
print("2)", format_equation_3(a2, b2, c2, d2))
print("3)", format_equation_3(a3, b3, c3, d3))
line()
A=[
[a1, b1, c1],
[a2, b2, c2],
[a3, b3, c3]
]
B = [d1, d2, d3]
D = det3(A)
print(f"Main determinant D = {D}")
if D == 0:
print("\nNo unique solution exists.")
print("This system may have no solution or infinitely many solutions.")
return
Dx = det3(replace_column(A, 0, B))
Dy = det3(replace_column(A, 1, B))
Dz = det3(replace_column(A, 2, B))
x = Dx / D
y = Dy / D
z = Dz / D
print(f"Dx = {Dx}")
print(f"Dy = {Dy}")
print(f"Dz = {Dz}")
print(f"\nx = Dx / D = {Dx} / {D} = {x}")
print(f"y = Dy / D = {Dy} / {D} = {y}")
print(f"z = Dz / D = {Dz} / {D} = {z}")
line()
print(f"Solution: x = {x}, y = {y}, z = {z}")
line()
# -----------------------------------
# Quiz mode for 2 variables
# -----------------------------------
def generate_2_var_question():
# choose solution first so equations come out nice
x = [Link](-5, 10)
y = [Link](-5, 10)
a1 = [Link](1, 6)
b1 = [Link](1, 6)
a2 = [Link](1, 6)
b2 = [Link](1, 6)
# ensure determinant not zero
while a1 * b2 - a2 * b1 == 0:
a2 = [Link](1, 6)
b2 = [Link](1, 6)
c1 = a1 * x + b1 * y
c2 = a2 * x + b2 * y
return (a1, b1, c1, a2, b2, c2, x, y)
def quiz_2_variable():
print("\n2-Variable Simultaneous Equations Quiz")
total = safe_int_input("How many questions? ")
score = 0
for i in range(1, total + 1):
a1, b1, c1, a2, b2, c2, x_ans, y_ans = generate_2_var_question()
line()
print(f"Question {i}")
print("1)", format_equation_2(a1, b1, c1))
print("2)", format_equation_2(a2, b2, c2))
user_x = safe_int_input("Enter x: ")
user_y = safe_int_input("Enter y: ")
if user_x == x_ans and user_y == y_ans:
print("Correct!")
score += 1
else:
print(f"Wrong. Correct answer: x = {x_ans}, y = {y_ans}")
line()
print(f"Final score: {score}/{total}")
line()
# -----------------------------------
# Quiz mode for 3 variables
# -----------------------------------
def generate_3_var_question():
x = [Link](-3, 6)
y = [Link](-3, 6)
z = [Link](-3, 6)
while True:
a1, b1, c1 = [Link](1, 4), [Link](1, 4), [Link](1, 4)
a2, b2, c2 = [Link](1, 4), [Link](1, 4), [Link](1, 4)
a3, b3, c3 = [Link](1, 4), [Link](1, 4), [Link](1, 4)
matrix = [
[a1, b1, c1],
[a2, b2, c2],
[a3, b3, c3]
]
if det3(matrix) != 0:
break
d1 = a1*x + b1*y + c1*z
d2 = a2*x + b2*y + c2*z
d3 = a3*x + b3*y + c3*z
return (a1, b1, c1, d1,
a2, b2, c2, d2,
a3, b3, c3, d3,
x, y, z)
def quiz_3_variable():
print("\n3-Variable Simultaneous Equations Quiz")
total = safe_int_input("How many questions? ")
score = 0
for i in range(1, total + 1):
data = generate_3_var_question()
a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, x_ans, y_ans, z_ans = data
line()
print(f"Question {i}")
print("1)", format_equation_3(a1, b1, c1, d1))
print("2)", format_equation_3(a2, b2, c2, d2))
print("3)", format_equation_3(a3, b3, c3, d3))
user_x = safe_int_input("Enter x: ")
user_y = safe_int_input("Enter y: ")
user_z = safe_int_input("Enter z: ")
if user_x == x_ans and user_y == y_ans and user_z == z_ans:
print("Correct!")
score += 1
else:
print(f"Wrong. Correct answer: x = {x_ans}, y = {y_ans}, z = {z_ans}")
line()
print(f"Final score: {score}/{total}")
line()
# -----------------------------------
# Menu
# -----------------------------------
def main_menu():
while True:
print("\nSimultaneous Equations Toolkit")
line()
print("1. Solve 2-variable simultaneous equations")
print("2. Solve 3-variable simultaneous equations")
print("3. Quiz on 2-variable equations")
print("4. Quiz on 3-variable equations")
print("5. Exit")
line()
choice = input("Choose an option: ").strip()
if choice == "1":
solve_2_variable()
elif choice == "2":
solve_3_variable()
elif choice == "3":
quiz_2_variable()
elif choice == "4":
quiz_3_variable()
elif choice == "5":
print("Goodbye.")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main_menu()