我在一行中输入两个变量时遇到问题

I have problem with inputing two variables in a single line

我正在解决 uri 问题 #1015 并遇到运行时错误 问题是我需要在一行中输入 2 个数字,但是我如何通过输入来做到这一点而不是扫描整行?

这是我的代码:

import math
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())

distance = math.sqrt((x2 - x1)**2 + (y2-y1) ** 2)
print("%.4f" % distance)

考虑获取输入行,将其拆分,验证您获得了正确数量和类型的值,然后存储它。

accepted_values = False
vals = []

while not accepted_values:

    inp = input("Please input four numbers: ")
    splt = inp.split()

    if len(splt) != 4:
        print("Please input exactly 4 numbers, separated by spaces")
        continue

    try:
        vals = [float(s) for s in splt]
        accepted_values = True
    except ValueError:
        print("Some or all values couldn't be converted to string")

x1, y1, x2, y2 = vals