Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 7th, 2013, 04:50 PM
 godzilla1160
Registered User

Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 54 m 45 sec
Reputation 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:

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:

elif m1!=m2 and poi_y>0 and poi_x<0:

elif m1!=m2 and poi_y<0 and poi_x<0:

elif m1!=m2 and poi_y<0 and poi_x>0:

#2
March 7th, 2013, 05:45 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,168
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 55 m 17 sec
Reputation Power: 455
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!

#3
March 7th, 2013, 05:50 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,168
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 55 m 17 sec
Reputation Power: 455
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): ")

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:
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:
elif poi_y>0 and poi_x<0:
elif poi_y<0 and poi_x<0:
elif poi_y<0 and poi_x>0:

#4
March 7th, 2013, 06:24 PM
 godzilla1160
Registered User

Join Date: Mar 2013
Posts: 6
Time spent in forums: 1 h 54 m 45 sec
Reputation Power: 0
Thank you but did you find the problem that is making my program output the wrong answers?

#5
March 7th, 2013, 06:45 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,168
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 55 m 17 sec
Reputation Power: 455
See the second post. It's short, hidden.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > I need help with basic programming!!(Python)