I’m trying to take input from user for 2 points and output the distance. I’m getting stuck on converting the input to a list for the output. I might be thinking about it the wrong way, any help to get me in the right direction is appreciated.
import math p1 = input("please enter x1 and y1: ") p2 = input("please enter x2 and y2: ") x1y1 = p1.split(',') x2y2 = p2.split(',') distance = math.sqrt( ((x1y1[0]-x2y2[0])**2)+((x1y1[1]-x2y2[1])**2) ) print(distance)
Advertisement
Answer
You can use a list comprehension to translate the inputs into int
s and then do a destructuring assignment to assign them to two different variables:
from math import sqrt [x1, y1] = [int(n) for n in input("please enter x1 and y1: ").split()] [x2, y2] = [int(n) for n in input("please enter x2 and y2: ").split()] print(f"Distance: {sqrt((x1-x2)**2+(y1-y2)**2)}")