尝试将输入转换为列表

我正在尝试从用户那里获取 2 个点的输入并输出距离。我陷入了将输入转换为输出列表的困境。我可能以错误的方式思考它,任何帮助我走向正确方向的帮助都是值得赞赏的。


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)


烙印99
浏览 83回答 5
5回答

鸿蒙传说

您可以使用列表理解将输入转换为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)}")

偶然的你

首先将每个元素转换为 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(',')]

慕码人8056858

这可以帮助:from math import sqrtxi, 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))

守着一只汪

import mathp1 = 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。

心有法竹

您可以使用map()轻松转换为intimport mathp1 = 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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python