HELP PLS! While else loop problem in python

< hi all,

I am trying to program a quadratic equation solver so that it continues infinitely while the det>=0, and stops when det<0. this is the code, can someone please help me with that?

at the moment, it asks to input a b and c after each input however only gives me the calculation for the first input no matter what a b and c in consequitive inputs are. It has to ask to input a b and c after each calculation except for when det<0 .

Thanks

import sys
import math
line = raw_input("input a b c: ")
textnums = line.split();
if(len(textnums) != 3) :
	print "Not 3 items on input line"
	sys.exit()
try:
	a = float(textnums[0])
except ValueError:
	print "First item is not a number"
	sys.exit()

try:
	b = float(textnums[1])
except ValueError:
	print "Second item is not a number"
	sys.exit()

try:
	c = float(textnums[2])
except ValueError:
	print "Third item is not a number"
	sys.exit()

det = (b*b) - (4*a*c);
det = 0.0

while (det >= 0.0):
	line = raw_input("input a b c: ")
	textnums = line.split();

	if(det > 0.0):
		r1 = (-b + math.sqrt(det)) / (2.0 * a),
		r2 = (-b - math.sqrt(det)) / (2.0 * a)
		print "the coefficients",a,b,c,"have 2 real roots",r1,r2
	elif(det == 0.0):
		print "the coefficients",a,b,c," have 1 real root ",-b/(2.0 * a)
else:
	print "the coefficients",a,b,c," have no real roots"
	sys.exit()

Hi @scriptsurfer53715,

You could use a while True loop, with break to exit the loop wherever necessary.

See the following, adapted or Python 3 …

import sys
import math
while True:
    line = input("input a b c: ")
    textnums = line.split();
    if(len(textnums) != 3):
        print("Not 3 items on input line")
        sys.exit()
    try:
        a = float(textnums[0])
    except ValueError:
        print("First item is not a number")
        break
    try:
        b = float(textnums[1])
    except ValueError:
        print("Second item is not a number")
        break
    try:
        c = float(textnums[2])
    except ValueError:
        print("Third item is not a number")
        break
    det = (b*b) - (4*a*c)
    if (det > 0.0):
        r1 = (-b + math.sqrt(det)) / (2.0 * a)
        r2 = (-b - math.sqrt(det)) / (2.0 * a)
        print("the coefficients {}, {}, and {} have 2 real roots: {}, {}".format(a, b, c,r1,r2))
    elif(det == 0.0):
        print("the coefficients {}, {}, and {} have 1 real root: {}".format(a, b, c, -b / (2.0 * a)))
    else:
        print("the coefficients {}, {}, {} have no real roots".format(a, b, c))
        break

There is still work to do. For example, if a is 0, you must prevent attempted division by 0.

2 Likes

it still doesnt work. it looks fine to me, but when i run it and input for example 2 4 2, it gives me an error of input xD

Are you running it using Python 2 or Python 3? To get my revision of your code to work in Python 2, you would need to make some changes to it. For instance, you would revert to using raw_input instead of input.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.