【发布时间】: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