【问题标题】:Python Quadratic Formula Not Calculating AccuratelyPython二次公式计算不准确
【发布时间】:2019-08-28 10:24:15
【问题描述】:

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

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.'

当我运行程序时,程序运行的最后 3 次输出是错误的,并且似乎将判别式设置为大于 0 的值,而我希望它根据计算出的判别式改变输出。提前致谢!

【问题讨论】:

  • 你输入的数字是整数还是小数
  • '...在我尝试过的所有输入中。 ' - 请提供一些示例输入以及您获得的输出与预期的输出。
  • 你的数字有多差?您尝试了哪些数字?
  • 所有输入都是整数。我用过:a=1,b=2,c=-8, and a=1,b=-12, and c=36
  • 请添加您的期望值,您获得的值并对其进行硬编码以使其成为minimal reproducible example

标签: python python-3.x python-module


【解决方案1】:

您似乎缺少一对括号。 这应该可以修复错误:

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: '))

【讨论】:

    【解决方案2】:

    找到了解决方案。我的判别函数在 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 社区的所有帮助和建议!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多