Hi,
Am a total newbie to programming & trying to learn python thro pdf tutorials.

I have this working code which uses four numbers as arguments to calculate the distance between 2 points.
Tried to unsuccessfully convert it to use Class.
Can some one guide me & can I request a simple explaination or some pointers or links to very simple:) explainations for Class,objects, etc.

#Program to calculate distance so that it takes two Points as arguments instead of four numbers.

import math

class Point:
    pass

first = Point()
second = Point()

first.x = float(input('Pls enter the x co-ordinate of the first point:')) #eg 3.0
first.y = float(input('Pls enter the y co-ordinate of the first point:')) #eg 4.0

second.x = float(input('Pls enter the x co-ordinate of the second point:')) #eg 6.0
second.y = float(input('Pls enter the y co-ordinate of the second point:')) #eg 8.0

def dist(x1,y1,x2,y2):
    dist = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
    return dist

dist = dist(first.x, first.y, second.x, second.y)

print(dist)

Thanks

Hi,
Am a total newbie to programming & trying to learn python thro pdf tutorials.

I have this working code which uses four numbers as arguments to calculate the distance between 2 points.
Tried to unsuccessfully convert it to use Class.
Can some one guide me & can I request a simple explaination or some pointers or links to very simple:) explainations for Class,objects, etc.

#Program to calculate distance so that it takes two Points as arguments instead of four numbers.

import math

class Point:
    pass

first = Point()
second = Point()

first.x = float(input('Pls enter the x co-ordinate of the first point:')) #eg 3.0
first.y = float(input('Pls enter the y co-ordinate of the first point:')) #eg 4.0

second.x = float(input('Pls enter the x co-ordinate of the second point:')) #eg 6.0
second.y = float(input('Pls enter the y co-ordinate of the second point:')) #eg 8.0

def dist(x1,y1,x2,y2):
    dist = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
    return dist

dist = dist(first.x, first.y, second.x, second.y)

print(dist)

Thanks

Hi,

This appears to be working.

#Program to calculate distance so that it takes two Points as arguments instead of four numbers.

import math

class Point:
    pass

first = Point()
second = Point()

first.x = float(input('Pls enter the x co-ordinate of the first point:')) #eg 3.0
first.y = float(input('Pls enter the y co-ordinate of the first point:')) #eg 4.0

second.x = float(input('Pls enter the x co-ordinate of the second point:')) #eg 6.0
second.y = float(input('Pls enter the y co-ordinate of the second point:')) #eg 8.0

def dist(original,final):
    dist = math.sqrt((original.x - final.x) * (original.x - final.x) + (original.y - final.y) * (original.y - final.y))
    return dist

dist = dist(first, second)

print(dist)

Request you guys to correct me if I got it wrong as presently I have a very shallow understanding of the basic concepts. Also "can I request a simple explaination or some pointers or links to very simple:) explainations for Class,objects, etc."

Thanks

Although your method works, you are not really using the features of class. You can google for "Byte of Python". It is in my opinion one of the best tutorials for beginners.

A good example of using a class is in this tutorial. Note especially the use of "self" and the indentation.

Hi Guys,

cghtkh

Although your method works, you are not really using the features of class. You can google for "Byte of Python". It is in my opinion one of the best tutorials for beginners.

I fully agree with you. I am trying to use class without really having understood what it is. I have "Byte of Python" by "Swaroop C H" but had not got down to reading it. Will start off immediately.

woooee

A good example of using a class is in this tutorial. Note especially the use of "self" and the indentation.

Thanks for the link.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.