Newbie programmer question


I've only just started learning programming (with Python) and I've bumped into this problem. Here's a program I wrote to come up with the roots to a quadratic equation:

import math

def main():
print("This program determines the real roots of a quadratic equation of the form: ax^2 + bx + c.")
a = input("Enter the valus of a: ")
b = input("Enter the valus of b: ")
c = input("Enter the valus of c: ")

discriminant = math.sqrt(b * b - 4 * a * c)
alpha = (-b + discriminant)/(2 * a)
beta = (b - discriminant)/(2 * a)

print("The roots of the equation are: ", alpha, beta)


And this leads to the error below:

[color=Red]Traceback (most recent call last):
File "", line 15, in
File "", line 9, in main
discriminant = math.sqrt(b * b - 4 * a * c)
TypeError: can't multiply sequence by non-int of type 'str'[/color]

I had to resort to adding an int() to all my variables when calculating the discriminant, alpha and beta variables. It's pretty troublesome having to type int() over and over again.

Is there a more elegant solution? Thanks (:
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!