- First you need to get the input coordinate from the user and check that it is a number so, after you get the input as an STR convert it to a FLOAT type in this process you can check if it is a number as it follows:
while True:
try:
x = float(input("Enter the X axis coordonate =>"))
print(type(x))
break
except ValueError:
print('Please try again')
while True:
try:
y = float(input("Enter the Y axis coordonate =>"))
print(type(y))
break
except ValueError:
print('Please try again')
cartesian = [x, y]
print('The input coordinate is:')
print('=>', cartesian)
note that the function WHILE allows you to enter the code into a cycle that checks the input until it is a variable that can be converted to a float, in this way you can work with negative numbers.
- Make the mathematical conversion using the Pitagoras theorem and trigonometric functions.
First, you have to import the math package, it’ll allow you to use the trigonometric functions, be aware that these functions work with radiants values, in this case, the code is written to print the angle in grades.
import math
size = (x**2 + y**2)**0.5
angle = math.atan(y / x) * 180 / math.pi
polar = [size, angle]
- After the conversion is done it is necessary to analyze the angle to show taking into account the quadrant where is located the cartesian coordinate.
if x > 0 and y > 0:
print('The coordonate is in the first quarter')
result = f'{size}, {angle}°'
print(result)
elif x < 0 and y > 0:
print('The coordonate is in the second quarter')
result = f'{size}, {180-angle*-1}°'
print(result)
elif x < 0 and y < 0:
print('The coordonate is in the third quarter')
result = f'{size}, {180+angle}°'
print(result)
elif x > 0 and y < 0:
print('The coordonate is in the forth quarter')
result = f'{size}, {360-angle*-1}°'
print(result)
thanks to those who take the time to see my post… keep learning
Curso de Fundamentos de Python
0 Comentarios
para escribir tu comentario