5 回答
TA贡献1865条经验 获得超7个赞
您可以使用列表理解将输入转换为ints,然后进行解构赋值以将它们分配给两个不同的变量:
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)}")
TA贡献1841条经验 获得超3个赞
首先将每个元素转换为 int:
p1 = input("please enter x1 and y1: ")
p2 = input("please enter x2 and y2: ")
x1y1 = [int(x) for x in p1.split(',')]
x2y2 = [int(y) for y in p2.split(',')]
TA贡献1803条经验 获得超6个赞
这可以帮助:
from math import sqrt
xi, yi = [int(i) for i in input().split()]
xf, yf = [int(i) for i in input().split()]
print(math.sqrt((xf-xi)**2 + (yf-yi)**2))
TA贡献1872条经验 获得超3个赞
import math
p1 = input("please enter x1 and y1: ")
p2 = input("please enter x2 and y2: ")
x1y1 = [int(num) for num in p1.split(',')]
x2y2 = [int(num) for num in p2.split(',')]
distance = math.sqrt( ((x1y1[0]-x2y2[0])**2)+((x1y1[1]-x2y2[1])**2) )
print(distance)
Str 应转换为 int。
TA贡献1866条经验 获得超5个赞
您可以使用map()轻松转换为int
import math
p1 = input("please enter x1 and y1: ")
p2 = input("please enter x2 and y2: ")
x1y1 = list(map(int, p1.split(',')))
x2y2 = list(map(int, p2.split(',')))
distance = math.sqrt( ((x1y1[0]-x2y2[0])**2)+((x1y1[1]-x2y2[1])**2) )
print(distance)
添加回答
举报