Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method That Calculates Slope In Python

I'm trying to learn Object Oriented Programming in Python. To do this I need to create a method that calculates the slope of a line, which joins the origin to a point. (I think) we're assuming that the origin is (0,0). For example:

Point(4, 10).slopeFromOrigin()
2.5
Point(12, -3).slopeFromOrigin()
-0.25
Point(-6, 0).slopeFromOrigin()
0

And we're using the equation slope = (Y2 - Y1) / (X2 - X1) to calculate the slope. Also, since dividing by 0 isn't allowed, we need to return None when the method fails. Here's what I tried:

class Point:

#Point class for representing and manipulating x,y coordinates

    def __init__(self, initX, initY):

#Create a new point at the given coordinates

        self.x = initX
        self.y = initY

    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def distanceFromOrigin(self):
        return ((self.x ** 2) + (self.y ** 2)) ** 0.5

#define a method called slopeFromOrigin here

    def slopeFromOrigin(self):

#set origin values for x and y (0,0)

        self.x = 0
        self.y = 0

#slope = (Y2 - Y1) / (X2 - X1)

        if (Point(x) - self.x) == 0:

            return None

        else: 

            return (Point(y) - self.y) / (Point(x) - self.x) 

#some tests to check our code

from test import testEqual
testEqual( Point(4, 10).slopeFromOrigin(), 2.5 )
testEqual( Point(5, 10).slopeFromOrigin(), 2 )
testEqual( Point(0, 10).slopeFromOrigin(), None )
testEqual( Point(20, 10).slopeFromOrigin(), 0.5 )
testEqual( Point(20, 20).slopeFromOrigin(), 1 )
testEqual( Point(4, -10).slopeFromOrigin(), -2.5 )
testEqual( Point(-4, -10).slopeFromOrigin(), 2.5 )
testEqual( Point(-6, 0).slopeFromOrigin(), 0 )

As you can see, I'm trying to say that we need the first parameter of Point to be x2, and the second parameter of Point to be y2. I tried it this way and got

NameError: name 'y' is not defined on line 32.

I also tried to get the index values of Point like this:

return (Point[0] - self.y / (Point[1] - self.x)

But that also gave me an error message:

TypeError: 'Point' does not support indexing on line 32

I'm not sure how to get the value of the x and y parameters from Point so that the method works when it's tested. Please share your suggestions if you have any. Thank you.

like image 306
SpeakInCode43 Avatar asked Mar 11 '23 09:03

SpeakInCode43


2 Answers

First problem

self.x = 0
self.y = 0

You just set the current point to the origin. Don't do that. The distance from the origin would then be 0...

Second problem Point(x) and Point(y) are not how you get the values for self.x and self.y.

Then, slope is simply "rise over run". Plus you want to return None when self.x == 0.

So, simply

def slopeFromOrigin(self):
    if self.x == 0:
        return None
    return self.y / self.x

Or even

def slopeFromOrigin(self):
    return None if self.x == 0 else self.y / self.x

Or let Python return None on its own

def slopeFromOrigin(self):
    if self.x != 0:
        return self.y / self.x

I think your confusion lies in that you think you need to somehow define "the origin". If you needed to do that, you would instead have this

origin = Point(0,0)
Point(-6, 0).slopeFromPoint(origin)
like image 119
OneCricketeer Avatar answered Mar 15 '23 04:03

OneCricketeer


        if (Point(x) - self.x) == 0:

        return None

    else: 

        return (Point(y) - self.y) / (Point(x) - self.x) 

As you can see, I'm trying to say that we need the first parameter of Point to be x2, and the second parameter of Point to be y2. I tried it this way and got

NameError: name 'y' is not defined on line 32.

You're trying to access the value of y, which is a global variable that you haven't assigned yet.

I also tried to get the index values of Point like this:

return (Point[0] - self.y / (Point[1] - self.x)

Two problems:

  1. "Point" is a class, not an object (which is an instance of an object).
  2. Even if you've put an object instead, Point is not an list-like object. In order to access an item using index like variableName[index], the class of the variableName must have an implementation for __getitem__(self, key). For example:

    >>> class GoodListClass:
    ...     def __init__(self, list):
    ...         self.myList = list
    ...     def __getitem__(self, key):
    ...         return self.myList[key]
    ...
    >>> class BadListClass:
    ...     def __init__(self, list):
    ...         self.myList = list
    ...
    >>> someList = range(10)
    >>> goodListObject = GoodListClass(someList)
    >>> badListObject = BadListClass(someList)
    >>> print(goodListObject[2])
    2
    >>> print(badListObject[2])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: BadListClass instance has no attribute '__getitem__'
    
like image 35
Caladbolgll Avatar answered Mar 15 '23 04:03

Caladbolgll