October 23rd, 2013, 07:57 PM

Whats the problem in my code?
import math
import time
a, b, c= input("enter coefficients of a, b and c separated by commas")
d= b**24*a*c # discriminant
e= math.sqrt d # square root of discriminant
def funct1 (a, b, c, e):
x= (b+ e)/2*a
if d<0:
print ("this equation has no real roots")
time.sleep(6)
elif d==0:
funct1(a, b, c, e)
print ("this equation has 2 real and equal roots")
time.sleep(6)
else:
funct1(a, b, c, e)
print (x)
e=e*1
funct1(a, b, c, e)
print (x)
time.sleep(6)
October 23rd, 2013, 09:04 PM

Unfortunately, the code as posted is unreadable; in order to display the code with the necessary indentation, you need to use the [CODE] or [HIGHLIGHT] tags around the code samples. Now, ordinarily, it would be possible for me to recover the formatting using the Quote Reply button, but in this case, the code is poorly and inconsistently that it is not possible to get a meaningful Python program from it.
Can you tell us what it is doing, and where it is failing?
October 23rd, 2013, 09:15 PM

Originally Posted by ScholRLEA
Unfortunately, the code as posted is unreadable; in order to display the code with the necessary indentation, you need to use the [CODE] or [HIGHLIGHT] tags around the code samples. Now, ordinarily, it would be possible for me to recover the formatting using the Quote Reply button, but in this case, the code is poorly and inconsistently that it is not possible to get a meaningful Python program from it.
Can you tell us what it is doing, and where it is failing?
sorry first time using the site
i updated the code in this reply
the problem is that my code is showing the negative root of the function as 0 for example if i input
1
0
1
the positive root comes out correct but the negative one comes out as 0
d is the discriminant and e is the root of d
if the discriminant is positive, i use function once with e and another time with positive e
can you also please use quote reply to see my indents
#This program calculates the roots of a quadratic equation
import math
import time
astr= input("enter coefficient of x^2 ")
bstr= input("enter coefficient of x ")
cstr= input ("enter constant ")
a= int(astr)#converts to integer
b= int(bstr)
c= int(cstr)
d= b**2  4*a*c # discriminant
def funct1 (a, b, c, e):
x= (b+ e)/2*a #actual formula
if x>0:
return (b+ e)/2*a
else:
return 0
if d<0:
print ("this equation has no real roots")
time.sleep(6)
elif d==0:
e= math.sqrt (d) # square root of discriminant
x=funct1(a, b, c, e)
print ("this equation has 2 real and equal roots")
print (x)
time.sleep(6)
else:
problem area Code:
e= math.sqrt (d) # square root of discriminant
x=funct1(a, b, c, e)
print (x)
x=funct1(a, b, c, e)
print (x)
time.sleep(6)
October 23rd, 2013, 11:08 PM

See my comments about repairing your program.
Code:
# runs in python 2, not in python3.
import cmath # use cmath instead of math
import time
def funct1(a, b, c, e): # fix the blocking.
return (b+e)/(2*a) # parentheses fix the order of operations.
# return the result
a, b, c = input("enter coefficients of a, b and c separated by commas")
d = b**24*a*c
e = cmath.sqrt(d) # parenthesize the function argument.
a_root = funct1(a,b,c,e)
another_root = funct1(a,b,c,e)
if d<0:
print ("this equation has no real roots")
print('one of the roots is',a_root)
elif d==0:
print ("this equation has 2 real and equal roots:",a_root)
else:
print(a_root)
print(another_root)
time.sleep(6)
[code]
Code tags[/code] are essential for python code and Makefiles!
October 25th, 2013, 03:18 PM

Originally Posted by b49P23TIvg
See my comments about repairing your program.
Code:
# runs in python 2, not in python3.
import cmath # use cmath instead of math
import time
def funct1(a, b, c, e): # fix the blocking.
return (b+e)/(2*a) # parentheses fix the order of operations.
# return the result
a, b, c = input("enter coefficients of a, b and c separated by commas")
d = b**24*a*c
e = cmath.sqrt(d) # parenthesize the function argument.
a_root = funct1(a,b,c,e)
another_root = funct1(a,b,c,e)
if d<0:
print ("this equation has no real roots")
print('one of the roots is',a_root)
elif d==0:
print ("this equation has 2 real and equal roots:",a_root)
else:
print(a_root)
print(another_root)
time.sleep(6)
Thanks alot
but why use cmath?
whats the difference
October 25th, 2013, 03:38 PM

rtfm
Code:
import math, cmath
print('cmath.sqrt(1):',cmath.sqrt(1))
print('\n'*2)
print('math.sqrt(1):',math.sqrt(1))
'''
Output of this program:
cmath.sqrt(1): 1j
Traceback (most recent call last):
File "p.py", line 5, in <module>
print('math.sqrt(1):',math.sqrt(1))
ValueError: math domain error
'''
[code]
Code tags[/code] are essential for python code and Makefiles!