Exercise: Abscissa and Ordinate

Exercise 11 Very easy

Prerequisites for the exercise

  1. Python String Basics
  2. All previous chapters

Objective

Extract out the abscissa and ordinate from an input co-ordinate point.

Description

A co-ordinate is a point on a Cartesian plane, having a value for the x-axis and a value for the y-axis.

It's usually denoted as follows:

(x, y)

The first number inside the parentheses is the value for x-axis, formally known as the abscissa. The second number is the value for the y-axis, formally known as the ordinate.

In this exercise, given a co-ordinate, we ought to extract out the abscissa and ordinate from it and print them, each on a new line.

Ask the user to input a co-ordinate with the following prompt message:

Enter a co-ordinate:

The input value should be of the following form: (x,y), where x is the abscissa and y is the ordinate.

There should be no space in the input value between x, , and y.

Once the co-ordinate is given, extract out the abscissa and ordinate from it and output them, each on a new line, after leaving a blank line to start with.

A couple of examples follow:

Enter a co-ordinate: (3,1)

Abscissa: 3
Ordinate: 1
Enter a co-ordinate: (-3.5,10.4)

Abscissa: -3.5
Ordinate: 10.4

Hints

Hint 1

Find the index of the comma (,) in the input value using the find() string method.

Hint 2

Slice the input value in two portions.

View Solution

New file

Inside the directory you created for this course on Python, create a new folder called Exercise-11-Abscissa-and-Ordinate and put the .py solution files for this exercise within it.

Solution

To solve this exercise, we just need to know two things: string slicing and find().

If you know both of these, you're good to go!

Step one is to set up the input prompt and receive the co-ordinate from the user, as stated. We'll call this point.

Let's get done with this first:

point = input('Enter the co-ordinate: ')

Step two is to find the index of the comma (,) in point.

This is required so that we could slice point in two halves at this position and then extract out the abscissa and ordinate from each half.

point = input('Enter the co-ordinate: ')

index = point.find(',')

Once the index of the comma is found, the next step is compute the abscissa.

The abscissa starts right after the first parenthesis (() in the string point and ends just before the comma (,). So to extract it, we write the following code:

point = input('Enter the co-ordinate: ')

index = point.find(',')
abscissa = point[1:index]

Next up, we move to extract the ordinate.

The ordinate begins right after the comma (,) and ends just before the ending parenthesis ()). This gives us the following code:

point = input('Enter the co-ordinate: ')

index = point.find(',')
abscissa = point[1:index]
ordinate = point[index + 1:-1]

With the abscissa and ordinate in hand, we make the desired output keeping in mind that we have to leave a blank line before doing so.

Altogether, we get the following code:

point = input('Enter the co-ordinate: ')

index = point.find(',')
abscissa = point[1:index]
ordinate = point[index + 1:-1]

print()
print('Abscissa:', abscissa);
print('Ordinate:', ordinate);