我在一行中输入两个变量有问题

时间:2019-09-12 11:28:25

标签: python-3.4

我正在解决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)

1 个答案:

答案 0 :(得分:0)

考虑取输入行,将其分割,确认您具有正确的数字和值的类型,然后再存储它。

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