[Tutor] next class problem

Roelof Wobben rwobben at hotmail.com
Sat Sep 18 19:20:03 CEST 2010



Hello, 
 
I have this exercise :
 
Rewrite the distance function from chapter 5 so that it takes two Points as parameters instead of four numbers.
 
I have this solution :
 
class Point:
     def __init__(self, x=0, y=0): 
         self.x = x
         self.y = y
     
def distance(p1,p2):
    dx = p2.x - p1.x
    dy = p2.y - p1.y
    dsquared = dx**2 + dy**2
    result = dsquared**0.5
    return result
P1 = Point()
P1.x = 3
P1.y = 3
P2 = Point()
P2.x = 6
P2.y = 7 
result = distance (P1,P2)
print result
 
 
Is this the correct solution ?
 
Roelof
  		 	   		  


More information about the Tutor mailing list