Python Forum
creating two points (x,y) and adding them together..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating two points (x,y) and adding them together..
#1
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
Yoriz write Nov-20-2021, 01:11 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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).
Reply
#3
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()))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding graph points and formating project_science 4 2,392 Jan-24-2021, 05:02 PM
Last Post: project_science
  How can I scroll over my data points when creating plots in Python? (I'm using Spyder moose 0 1,603 Nov-02-2020, 07:18 AM
Last Post: moose
  Adding markers to Folium map only adding last element. tantony 0 2,121 Oct-16-2019, 03:28 PM
Last Post: tantony
  Issue with ginput and adding points to an exisiting plot gaminer 0 1,616 Oct-12-2019, 07:08 PM
Last Post: gaminer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020