[Tutor] next class problem
Roelof Wobben
rwobben at hotmail.com
Sat Sep 18 19:50:45 CEST 2010
----------------------------------------
> Date: Sat, 18 Sep 2010 13:40:55 -0400
> From: bgailer at gmail.com
> To: rwobben at hotmail.com
> CC: tutor at python.org
> Subject: Re: [Tutor] next class problem
>
> On 9/18/2010 1:20 PM, Roelof Wobben wrote:
>>
>> 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 ?
>
> What is your criteria for "correct"?
>
> There is no one correct solution!
>
> You seem to be passing 2 points, as requested.
>
> Do you get the correct answer?
>
> Then it mus be correct.
>
> FWIW Python convention recommends names starting with lower case except
> for classes and constants.
>
> Therefore p1 and p2 are preferred to P1 and P2.
>
> Also why not initialize x and y thus:
> p1 = Point(3,3)
> That is what the __init__ is for.
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
Hello,
Thank you.
Learned another thing.
Roelof
More information about the Tutor
mailing list