March 7th, 2013, 03:50 PM

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)
March 7th, 2013, 04:45 PM

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!
March 7th, 2013, 04:50 PM

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!
March 7th, 2013, 05:24 PM

Thank you but did you find the problem that is making my program output the wrong answers?
March 7th, 2013, 05:45 PM

See the second post. It's short, hidden.
[code]
Code tags[/code] are essential for python code and Makefiles!