基本的python程序

时间:2013-03-07 22:06:01

标签: python

我试图完成该程序,但答案是错误的,我无法确定究竟是什么。

问题:给定两条线的方程(y = mx + b),确定两条线是平行的,相同的还是相交的。计算并输出交点。

我的代码:

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 = equation_1[x_1+1:]
b2 = equation_2[x_2+1:]

m1 = equation_1[equalsign_1+1:x_1]
m2 = 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 = float(b2)-float(b1)
    equation_intersect_x = float(m2)-float(m1) # equation_intersect_x = float(m1)-float(m2)

    poi_x = float(equation_intersect_y)/float(equation_intersect_x)
    poi_y = float(b1)*float(poi_x)+float(m1)`

3 个答案:

答案 0 :(得分:1)

您用于计算poi_x的等式是错误的。此外,您的代码用于计算poi_y的公式已互换mb。这是一个稍微修改过的代码应该有所帮助:

#! /usr/bin/env python
equation_1 ="y=2x+3"
equation_2 ="y=-0.5x+7"

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])

print m1,b1,m2,b2

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 = m1 - m2

    poi_x = equation_intersect_y/equation_intersect_x
    poi_y = m1*poi_x+b1

    print poi_x, poi_y

输出结果为:

2.0 3.0 -0.5 7.0
1.6 6.2

这是一个稍微好一点的代码,可以减少重复:

#! /usr/bin/env python
def parse_equation_string(eq_string):
    x_pos = eq_string.find('x')
    equal_pos = eq_string.find('=')

    b = float(eq_string[x_pos+1:])
    m = float(eq_string[equal_pos+1:x_pos])
    return m, b

def get_point_of_intersection(line1, line2):
    m1, b1 = line1
    m2, b2 = line2

    if m1==m2 and b1!=b2:
        return "The lines are parallel. "

    elif m1==m2 and b1==b2:
        return "The lines are the same. "

    else:
        equation_intersect_y = b2 - b1
        equation_intersect_x = m1 - m2

        poi_x = equation_intersect_y/equation_intersect_x
        poi_y = m1*poi_x+b1

        return poi_x, poi_y

equation_1 = "y=2x+3"
equation_2 = "y=-0.5x+7"

line_1 = parse_equation_string(equation_1)
line_2 = parse_equation_string(equation_2)

print line_1, line_2
print get_point_of_intersection(line_1, line_2)

输出结果为:

(2.0, 3.0) (-0.5, 7.0)
(1.6, 6.2)

答案 1 :(得分:0)

应该不是

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

b1 = equation_1[plus_1+1:]
b2 = equation_2[plus_2+1:]

或者

b1 = equation_1[x_1+2:]
b2 = equation_2[x_2+2:]

我也认为

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

应该是

m1 = equation_1[equalsign_1+1:x_1-1]
m2 = equation_2[equalsign_2+1:x_2-1]

答案 2 :(得分:0)

前几条建议:

在执行操作之前(在第一个“if”语句之前)将公式打印回用户:

print "Equation 1  y={}x+{}".format(m1, b1)
print "Equation 2  y={}x+{}".format(m2, b2)

re或字符串函数split和strip可能比字符串索引更容易:

m1 = equation_1.split('=')[1].split('x')[0]
b1 = equation_1.split('=')[1].split('+')[1]

在较难的测试用例之前给程序一些简单的测试用例:     1:y = 0x + -3     2:y = 1x + 0     在X = 3

处相交
1: y=-1x+0
2: y=0x+3
intersects at  X = -3


1: y=2x+2
2: y=-2x+0
intersects at X = -0.5

现在剩下的只是代数:

首先用手做一个硬盘:

假设它们不相同或相同:     找到x1 = x2和y1 = y2的点 找到两个Y相等的X:因此:
    (m1)* x1 + b1 = y1 = y2 =(m2)* x2 + b2     重写以找到X:(m1)* x1 + b1 =(m2)* x2 + b2     但在兴趣点(X)x1 = x2     重写:(m1 + m2)* X = b2 -b1     重写:X =(b2-b1)/(m1 + m2)

现在我们可以看到这与您的equation_intersect x公式不匹配。