#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    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**2-4*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)
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by Schol-R-LEA
    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)
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    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**2-4*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!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0
    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**2-4*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
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    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!

IMN logo majestic logo threadwatch logo seochat tools logo