Making a program to solve a quadratic equation

This is the place for queries that don't fit in any of the other categories.

Making a program to solve a quadratic equation

Postby edwunclerlll » Wed Sep 04, 2013 7:56 pm

So here is my dilemma... I have been trying to write the code to solve a quadratic equation by entering a, b, and c yet I get an invalid syntax error could someone help?

Code: Select all
from math import *

def quadraticRoots( a , b , c ):
    print( ax ** 2 + bx + c )
    d = b ** 2 - 4 * a * c
    if d < 0:
       print ( "This equation has no solution" )
    elif d == 0:
       x = ( -b + sqrt (b ** 2 - 4 * a * c )) / ( 2 * a )
       print (" This equation has one solution: " , x)
    else:
       x1 = ( -b - sqrt ( d )) / ( 2 * a)
       x2 = ( -b - sqrt ( d )) / ( 2 * a)
       print ("This equation has two solutions:" x1, x2)
Last edited by Mekire on Wed Sep 04, 2013 10:35 pm, edited 1 time in total.
Reason: First post lock. COde tags added.
edwunclerlll
 
Posts: 1
Joined: Wed Sep 04, 2013 7:23 pm

Re: Making a program to solve a quadratic equation

Postby Mekire » Wed Sep 04, 2013 10:43 pm

Hello and welcome to the forum. Please make sure you read this thread carefully.

Your initial syntax error is due to forgetting a comma in your final print line. There are further issues after this. Please report back.

-Mek
User avatar
Mekire
 
Posts: 1140
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Making a program to solve a quadratic equation

Postby Somelauw » Sat Sep 07, 2013 10:22 am

Just read the error message. It shows you at what position something is wrong.

Code: Select all
$ python op.py
  File "op.py", line 14
    print ("This equation has two solutions:" x1, x2)
                                               ^
SyntaxError: invalid syntax


About this position a comma is missing.
Last edited by Somelauw on Sat Sep 07, 2013 12:14 pm, edited 1 time in total.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 75
Joined: Tue Feb 12, 2013 8:30 pm

Re: Making a program to solve a quadratic equation

Postby el_manu » Sat Sep 07, 2013 10:28 am

you need one just before that after your quotes
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am


Return to General Coding Help

Who is online

Users browsing this forum: Keepsake, snippsat, Yoriz and 4 guests