这是我运行程序的主文件:
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的值时,我希望它根据计算出的判别式来更改输出。预先感谢!
答案 0 :(得分: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: '))
答案 1 :(得分:0)
找到了解决方案。我的判别函数在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社区获得的所有帮助和建议!