Python - 二次方程PLS响应

时间:2016-10-18 22:34:25

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

我们必须制作一个解决二次方程式的程序,我做了那个部分,但我们还必须包含一个用户输入他们认为正确答案的部分,如果它们是正确的,程序应输出类似“你是对的”。如果它们错了,它应该输出类似“你错了”的东西,然后在它下面输出正确的答案。这是我到目前为止可以有人请为我合并这部分我的代码如下。

print "This program can be used to solve quadratic equations"

print "Below, please input values for a, b, and c" 

print "\n----------------------------\n"

import math


for i in range(39479):

    a = float(raw_input("Enter a value for a: "))
    b = float(raw_input("Enter a value for b: "))
    c = float(raw_input("Enter a value for c: "))

    if a==0:
        print "Please input a value greater than or less than 0 for a"
    else:
        break

print "\n----------------------------\n"


discriminant = (b**2)-(4*(a*c)) 

if discriminant < 0:

    print ("This equation has no real solution")

elif discriminant == 0:

    repeated_solution = (-b-math.sqrt(b**2-4*a*c))/2*a
    print ("This equation has one, repeated solution: "), repeated_solution

else:

    root_1 = (-b+math.sqrt(discriminant))/(2*a)
    root_2 = (-b-math.sqrt(discriminant))/(2*a)
    print "This equation has two solutions:", root_1, " and/or", root_2

2 个答案:

答案 0 :(得分:0)

我知道你在处理比较部分时遇到了问题。所以你可以添加一个真正的基本验证,看起来像这样: d = float(raw_input("what is your answer: ")) if d*d=root_1*root_1 | d*d=repeated_solution*repeated_solution: print('correct') else: print('false') 在数学上你比较答案的平方和结果两个案例导致相同的机制。避免负面格式的问题。 并在打印结果时删除该部分...

答案 1 :(得分:0)

print "This program can be used to solve quadratic equations"

print "Below, please input values for a, b, and c" 

print "\n----------------------------\n"

import math


for i in range(39479):

    a = float(raw_input("Enter a value for a: "))
    b = float(raw_input("Enter a value for b: "))
    c = float(raw_input("Enter a value for c: "))

    if a==0:
        print "Please input a value greater than or less than 0 for a"
    else:
        break

print "\n----------------------------\n"


discriminant = (b**2)-(4*(a*c)) 

if discriminant < 0:

    print ("This equation has no real solution")

elif discriminant == 0:

    repeated_solution = format((-b-math.sqrt(b**2-4*a*c))/2*a,'.3f')
    guess=format(float(raw_input("This equation has one, repeated solution, Make a guess:")),'.3f')
    if guess==repeated_solution:
        print "You are correct.WOW!"
        print ("This equation has one, repeated solution: "), repeated_solution        
    else:
        print "Wrong Answer"
else:
    root_1 = format((-b+math.sqrt(discriminant))/(2*a),'.3f')
    root_2 = format((-b-math.sqrt(discriminant))/(2*a),'.3f')
    guess1=format(float(raw_input("This equation has two solutions, Make a guess for solution1:")),'.3f')
    guess2=format(float(raw_input("This equation has two solutions, Make a guess for solution2:")),'.3f')
    if set([root_1,root_2]).issubset( [guess1,guess2]):
        print "You are correct.WOW!"
        print "This equation has two solutions:", root_1, " and/or", root_2
    else:
        print "Wrong Answer"