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

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    I need help with basic programming!!(Python)


    Hey guys! I would really appreciate it if someone helped me! I tried to complete the program but the answer is wrong. Something may be wrong with the calculations but im not sure. Please help!

    QUESTION: Given the equations of two lines (y=mx+b)
    Determine if the two lines are parallel, the same or intersect.
    Calculate and output the point of intersection.

    MY CODE:

    equation_1 =raw_input("Please enter the equation of your 1st line(y=mx+b): ")
    equation_2 =raw_input("Please enter the equation of your 2nd line(y=mx+b): ")

    plus_1 = equation_1.find('+')
    plus_2 = equation_2.find('+')

    x_1 = equation_1.find('x')
    x_2 = equation_2.find('x')

    equalsign_1 = equation_1.find('=')
    equalsign_2 = equation_2.find('=')

    b1 = float(equation_1[x_1+1:])
    b2 = float(equation_2[x_2+1:])

    m1 = float(equation_1[equalsign_1+1:x_1])
    m2 = float(equation_2[equalsign_2+1:x_2])


    if m1==m2 and b1!=b2:
    print "Your equations are parallel. "

    elif m1==m2 and b1==b2:
    print "Your equations are the same. "

    else:
    equation_intersect_y = (b2)-(b1)
    equation_intersect_x = (m2)-(m1)
    poi_x = (equation_intersect_y)/(equation_intersect_x)
    poi_y = (m1)*(poi_x)+(b1)

    if m1!=m2 and poi_y>0 and poi_x>0:
    print "Your equations intersect in Quadrant 1 at (%.2f,%.2f)"%(poi_x,poi_y)

    elif m1!=m2 and poi_y>0 and poi_x<0:
    print "Your equations intersect in Quadrant 2(%.2f,%.2f)"%(poi_x,poi_y)

    elif m1!=m2 and poi_y<0 and poi_x<0:
    print "Your equations intersect in Quadrant 3(%.2f,%.2f)"%(poi_x,poi_y)

    elif m1!=m2 and poi_y<0 and poi_x>0:
    print "Your equations intersect in Quadrant 4(%.2f,%.2f)"%(poi_x,poi_y)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    You need negation.
    Code:
        poi_x = -(equation_intersect_y)/(equation_intersect_x)
    Please use code tags, see my signature.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    You can reduce the number of comparisons, I think this is simpler logic.
    Code:
    equation_1 =raw_input("Please enter the equation of your 1st line(y=mx+b): ")
    equation_2 =raw_input("Please enter the equation of your 2nd line(y=mx+b): ")
    
    plus_1 = equation_1.find('+')
    plus_2 = equation_2.find('+')
    
    x_1 = equation_1.find('x')
    x_2 = equation_2.find('x')
    
    equalsign_1 = equation_1.find('=')
    equalsign_2 = equation_2.find('=')
    
    b1 = float(equation_1[x_1+1:])
    b2 = float(equation_2[x_2+1:])
    
    m1 = float(equation_1[equalsign_1+1:x_1])
    m2 = float(equation_2[equalsign_2+1:x_2])
    
    
    if m1==m2:
        if b1!=b2:
            print "Your equations are parallel."
        else:
            print "Your equations are the same."
    else:
        equation_intersect_y = (b2)-(b1)
        equation_intersect_x = (m2)-(m1)
        poi_x = -(equation_intersect_y)/(equation_intersect_x)
        poi_y = (m1)*(poi_x)+(b1)
        if poi_y>0 and poi_x>0:
            print "Your equations intersect in Quadrant 1 at (%.2f,%.2f)"%(poi_x,poi_y)
        elif poi_y>0 and poi_x<0:
            print "Your equations intersect in Quadrant 2(%.2f,%.2f)"%(poi_x,poi_y)
        elif poi_y<0 and poi_x<0:
            print "Your equations intersect in Quadrant 3(%.2f,%.2f)"%(poi_x,poi_y)
        elif poi_y<0 and poi_x>0:
            print "Your equations intersect in Quadrant 4(%.2f,%.2f)"%(poi_x,poi_y)
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0
    Thank you but did you find the problem that is making my program output the wrong answers?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    See the second post. It's short, hidden.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo