-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgebra: 2 x 2 linear equations
72 lines (58 loc) · 2 KB
/
Algebra: 2 x 2 linear equations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
Larry McQuaid
MET CS 521
10/11/18
Homework 6, Question 7.7
Algebra: 2 x 2 linear equations
"""
class LinearEquations:
#Initializing the linear equation variables
def __init__(self, a=0, b=0, c=0, d=0, e=0, f=0):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
#Accessor methods for the linear equation variables
def getA(self):
return self.a
def getB(self):
return self.b
def getC(self):
return self.c
def getD(self):
return self.d
def getE(self):
return self.e
def getF(self):
return self.f
#Determining if the problem is solvable
def isSolvable(self):
if ((a*d) - (b*c)) != 0:
return True
else:
print('The equation has no solution')
#Solving for x
def getX(self):
return float(((e*d) - (b*f)) / ((a*d) - (b*c)))
#Solving for y
def getY(self):
return float(((a*f) - (e*c)) / ((a*d) - (b*c)))
try:
#User is prompted to enter inputs for the linear equation
a = float(input('Please enter a number for a: '))
b = float(input('Please enter a number for b: '))
c = float(input('Please enter a number for c: '))
d = float(input('Please enter a number for d: '))
e = float(input('Please enter a number for e: '))
f = float(input('Please enter a number for f: '))
#Fill the class with the user entered inputs above
LinearEquations_1 = LinearEquations(a, b, c, d, e, f)
#Call the isSolvable function and if true, print output
if LinearEquations_1.isSolvable():
print('\nThe value for x is: {:.2f} \nThe value for y is: {:.2f}' \
.format(LinearEquations_1.getX(), LinearEquations_1.getY()))
except: #Displays if the user tries to input anything other than a number
print('Something went wrong, please try again')