Quantcast
Viewing all articles
Browse latest Browse all 140

Newbie programmer question

Hi,

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.")
print
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)

main()

And this leads to the error below:

[color=Red]Traceback (most recent call last):
File "C:UserszlyangDesktopha.py", line 15, in
main()
File "C:UserszlyangDesktopha.py", 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 (:

Viewing all articles
Browse latest Browse all 140

Trending Articles