Python Forum
creating two points (x,y) and adding them together.. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: creating two points (x,y) and adding them together.. (/thread-35592.html)



creating two points (x,y) and adding them together.. - azhag - Nov-20-2021

Hi,
I have started learning python this week and am trying to write a simple script. I wish to use a function to create two user defined points A and B, each with two coordinates (x, y). I then want to use another function to add the coordinates together (x1+x2, y1+y2)

Here is what i have...
def createPoint(x,y):
    return [x,y]
def additionAB (x1, x2,  y1, y2):
    c = x1 + x2, y1 + y2
    return c;
x1 = input ('Enter a value x of point a')
y1 = input ('Enter a value y of point a')
x2 = input ('Enter a value x of point b')
y2 = input ('Enter a value y of point b')
print (c)
As I'm new here I have no idea what has gone wrong. I wish to keep it as simple as possible and easy to understand. Any help would be greatly appreciated! Thanks in advance


RE: creating two points (x,y) and adding them together.. - deanhystad - Nov-20-2021

input() returns a str. You will need to convert that to a number.

Wrap any code inside Python tags (there is a button in the editor). If you have an error, post the error and the entire traceback. Wrap error in error tags (another button in the editor).


RE: creating two points (x,y) and adding them together.. - ghoul - Nov-20-2021

You probably want this to create the points:

def createPoints():
    x1 = int(input ('Enter a value x of point a '))
    y1 = int(input ('Enter a value y of point a '))
    x2 = int(input ('Enter a value x of point b '))
    y2 = int(input ('Enter a value y of point b '))

    return [[x1, y1], [x2, y2]]
To add the points:

def additionAB (points):
    c = points[0][0] + points[1][0], points[0][1] + points[1][1]
    return c
The call them

print(additionAB(createPoints()))