This is my main file that runs program:
import math
import Disc
def main():
coeffA = int(input('Enter the coefficient A: '))
coeffB = int(input('Enter the coefficient B: '))
coeffC = int(input('Enter the coefficient C: '))
disc = Disc.discriminant(coeffA, coeffB, coeffC)
while coeffA != 0:
if disc > 0:
solutionOne = (-coeffB + math.sqrt(disc)) / (2 * coeffA)
solutionTwo = (-coeffB - math.sqrt(disc)) / (2 * coeffA)
print('Solutions are: ' + str(solutionOne) + ' and ' + str(solutionTwo))
coeffA = int(input('Enter the coefficient A: '))
coeffB = int(input('Enter the coefficient B: '))
coeffC = int(input('Enter the coefficient C: '))
elif disc == 0:
solutionOne = -coeffB / (2 * coeffA)
print('Solution is: ' + str(solutionOne))
coeffA = int(input('Enter the coefficient A: '))
coeffB = int(input('Enter the coefficient B: '))
coeffC = int(input('Enter the coefficient C: '))
elif disc < 0:
print('Equation has two complex roots.')
coeffA = int(input('Enter the coefficient A: '))
coeffB = int(input('Enter the coefficient B: '))
coeffC = int(input('Enter the coefficient C: '))
else:
print('Program ended.')
# End of the main function
main()
Here is the Disc.py file where the discriminant value is calculated to be used in main() function:
def discriminant(coeffA, coeffB, coeffC):
value = (coeffB ** 2) - (4 * coeffA * coeffC)
return value
This is the output when running the program:
Enter the coefficient A: 1
Enter the coefficient B: 2
Enter the coefficient C: -8
Solutions are: 2.0 and -4.0
Enter the coefficient A: 1
Enter the coefficient B: -12
Enter the coefficient C: 36
Solutions are: 9.0 and 3.0
Enter the coefficient A: 2
Enter the coefficient B: 9
Enter the coefficient C: -5
Solutions are: -0.75 and -3.75
Enter the coefficient A: 4
Enter the coefficient B: 6
Enter the coefficient C: 20
Solutions are: 0.0 and -1.5
Enter the coefficient A: 0
Enter the coefficient B: 0
Enter the coefficient C: 0
Program ended.
I am expecting the following roots with the inputs above:
Run1: 2, -4
Run2: 6
Run3: .5, -5
Run4: 'Equation has two complex roots.'
When I run the program, the output is wrong for the last 3 times the program runs, and seems to be setting the discriminant equal to values greater than 0, when I expect it to change the output based off the discriminant calculated. Thanks in advance!
Found the solution. My discriminant function was outside of the while statement, so when the 3 variables were entered from the user, it was keeping the first discriminant calculated in each loop, which was why the output was producing 2 answers each run. By changing the position of the discriminant function to inside of the while statement, it now recalculates the discriminant for each loop for the while statement. This is the correct code for this problem:
import math
import Disc
def main():
coeffA = int(input('Enter the coefficient A: '))
while coeffA != 0:
coeffB = int(input('Enter the coefficient B: '))
coeffC = int(input('Enter the coefficient C: '))
disc = Disc.discriminant(coeffA, coeffB, coeffC)
if disc > 0:
solutionOne = (-coeffB + math.sqrt(disc)) / (2 * coeffA)
solutionTwo = (-coeffB - math.sqrt(disc)) / (2 * coeffA)
print('Solutions are: ' + str(solutionOne) + ' and ' + str(solutionTwo))
coeffA = int(input('Enter the coefficient A: '))
elif disc == 0:
solutionOne = -coeffB / (2 * coeffA)
print('Solution is: ' + str(solutionOne))
coeffA = int(input('Enter the coefficient A: '))
elif disc < 0:
print('Equation has two complex roots.')
coeffA = int(input('Enter the coefficient A: '))
print('Program ended.')
# End of the main function
The correct output code produced is the following:
Enter the coefficient A: 1
Enter the coefficient B: 2
Enter the coefficient C: -8
Solutions are: 2.0 and -4.0
Enter the coefficient A: 1
Enter the coefficient B: -12
Enter the coefficient C: 36
Solution is: 6.0
Enter the coefficient A: 2
Enter the coefficient B: 9
Enter the coefficient C: -5
Solutions are: 0.5 and -5.0
Enter the coefficient A: 4
Enter the coefficient B: 6
Enter the coefficient C: 20
Equation has two complex roots.
Enter the coefficient A: 0
Program ended.
Thank you for all the help and advice from the Stack Overflow community!