Python 二次公式计算不准确

Python Quadratic Formula Not Calculating Accurately

这是我的主文件,运行s 程序:

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()

这里是 Disc.py 文件,其中计算了要在 main() 函数中使用的判别值:

def discriminant(coeffA, coeffB, coeffC):

    value = (coeffB ** 2) - (4 * coeffA * coeffC)

    return value

这是 运行 运行程序时的输出:

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.

我期望使用上述输入得到以下根:

Run1: 2, -4

Run2: 6

Run3: .5, -5

Run4: 'Equation has two complex roots.'

当我 运行 程序时,程序 运行s 的最后 3 次输出是错误的,并且似乎将判别式设置为大于 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: '))

找到解决方案。我的判别函数在 while 语句之外,所以当用户输入 3 个变量时,它会保留在每个循环中计算的第一个判别式,这就是输出每个产生 2 个答案的原因 运行。通过将判别函数的位置更改为 while 语句内部,它现在为 while 语句的每个循环重新计算判别式。这是此问题的正确代码:

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

生成的正确输出代码如下:

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.

感谢 Stack Overflow 社区提供的所有帮助和建议!