J
J
JonathanXate2013-12-26 16:07:58
Python
JonathanXate, 2013-12-26 16:07:58

How to write a simple quadratic equation program in Python?

I recently started learning python from Lutz's book and the codecademy course (my first experience in programming). I tried to write a simple program for calculating quadratic equations:

a = input('Введите а:')
b = input('Введите b:')
c = input('Введите c:')
if len(a) > 0 and len(b) > 0 and len(c) > 0 and not a.isalpha() and not b.isalpha() and not c.isalpha():
    d = int(b) ** 2 - 4 * int(a) * int(c)
    if d > 0 or d == 0:
        import math
        dd = math.sqrt(d)
        x = - int(b) - int(dd) / (int(a) * 2)
        x1 = - int(b) + int(dd) / (int(a) * 2)
        print (x, x1)
    else:
        print ('Рещений не имеет')
else:
    print ('Введите только числа')

Actually, the x's are calculated incorrectly, before that everything is done correctly.
Please explain what is my mistake?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FloppyFormator, 2013-12-26
@JonathanXate

x = ( -int(b) - int(dd) ) / (int(a) * 2)
x1 = ( -int(b) + int(dd) ) / (int(a) * 2)

P
Pavel Zagrebelin, 2013-12-28
@Zagrebelion

Reading a quadratic equation in integers is so unusual.

A
Artem Kalachyan, 2013-12-26
@Bringoff

If d = 0, then it is desirable to display one root, don't you think?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question