Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Python
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)
if d > 0:
     sol1 = (-b-cmath.sqrt(d))/(2*a)
     sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
else:
                        rp = -b/2a
                        ip = cmath.sqrt(d)/2a
                        print('The Solutions are {0} and {1}'.format(rp,ip))


What I have tried:

Tried many things like changing shifts and all
Posted
Updated 19-Jan-21 0:00am
v2
Comments
Sandeep Mewara 19-Jan-21 0:06am    
Your query is not clear. Can you elaborate or rephrase on what is your issue herE?
KarstenK 19-Jan-21 5:39am    
Python is using EXTREME STRICT idention to structure code. If you dont respect it, you will suffer.

Quote:
I am getting indentation in this basic code in Python can someone explain this ?

Did you forgot the word 'Error' ?
Try
Python
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)
if d > 0:
     sol1 = (-b-cmath.sqrt(d))/(2*a)
     sol2 = (-b+cmath.sqrt(d))/(2*a)
     print('The solution are {0} and {1}'.format(sol1,sol2))
else:
    rp = -b/2a
    ip = cmath.sqrt(d)/2a
    print('The Solutions are {0} and {1}'.format(rp,ip))

Beware, other errors in your code.
You forgot to handle the case when there is no solution.
 
Share this answer
 
v2
Comments
CPallini 19-Jan-21 5:39am    
5.'You forgot to handle the case when there is no solution'
Actually he blundered on reporting the complex solutions (and forgot to handle the special case of coincident solutions).
Moreover using complex math he didn't need to discriminate on the discriminant (pardon the pun).
Patrice T 19-Jan-21 7:37am    
Thank you.
In Python indentation is significant: identically indented lines of code form a block of code which is executed together.
So when you do an if ... else statement, all teh lines that are executed when the condition is true must share teh same indentation:
Python
if condition:
   line 1
   line 2
else:
   line 3
   line 4
line 5
If the condition is true, both line 1 and line 2 will be executed, if it isn't, then both line3 and line 4 will be executed.

Your code has a second indented block in the middle:
Python
if condition:
   line 1
   line 2
line 5
else:
   line 3
   line 4
line 5

Which means that the system cannot work out what the else code is doing there as the if statement was ended by line 5

(This is one of the stupid things about Python, and which causes the most problems: because any whitespace is taken as significant it is possible to have two adjacent lines of code that look identical but are differently indented because spaces and tabs are not the same. Make sure you use an editor that replaces tabs with spaces or it can get very, very confusing.)
 
Share this answer
 
Comments
CPallini 19-Jan-21 5:40am    
5.
Complex math takes care of the discriminant sign, so you may write
Python
import cmath
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('The Solutions are {0} and {1}'.format(sol1, sol2))



Using the traditional ('real' math) approach:

Python
import math
a = 1
b = -2
c = 4

# calculate the discriminant
d = (b**2) - (4*a*c)
if d > 0:
     sol1 = (-b-math.sqrt(d))/(2*a)
     sol2 = (-b+math.sqrt(d))/(2*a)
     print('The solutions are {0} and {1}'.format(sol1,sol2))
elif d == 0:
     sol12 = -b/(2*a)
     print('The solutions are {0}, one and the same'.format(sol12))
else:
     # here  d <0
     rp = -b/(2*a)
     ip = math.sqrt(-d)/(2*a)
     print('The Solutions are {0}+{1}j and {0}-{1}j'.format(rp,ip))
 
Share this answer
 
Comments
Patrice T 19-Jan-21 7:37am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900