Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我该如何改进它,以便将距离添加到列表中,然后添加总和_Python - Fatal编程技术网

Python 我该如何改进它,以便将距离添加到列表中,然后添加总和

Python 我该如何改进它,以便将距离添加到列表中,然后添加总和,python,Python,我一直在努力想,一旦用户输入999,如何打破循环,这并不会因为某种原因而停止循环,另一个问题是list distList用于存储用户输入x和y值时行驶的距离,但它会覆盖第一个输入。您的坐标()函数的while循环:您从未调用步骤(),因此您从未遇到if检查while循环 您的coord()while循环中也没有任何条件(例如if),这意味着无法“突破”该循环 请查看此稍微更新的代码: x1=0.00 y1=0.00 distList=[] def坐标(x1,y1): 尽管如此: x2=int(输

我一直在努力想,一旦用户输入999,如何打破循环,这并不会因为某种原因而停止循环,另一个问题是list distList用于存储用户输入x和y值时行驶的距离,但它会覆盖第一个输入。

您的
坐标()
函数的while循环:您从未调用
步骤()
,因此您从未遇到
if
检查while循环

您的
coord()
while循环中也没有任何条件(例如
if
),这意味着无法“突破”该循环

请查看此稍微更新的代码:

x1=0.00
y1=0.00
distList=[]
def坐标(x1,y1):
尽管如此:
x2=int(输入(“输入x:”)
y2=int(输入(“输入y:”)
#通过提交“999”让用户退出此处
如果x2==999或y2==999:
打破
x1=x2
y1=y2
距离=((x2-x1)**2+(y2-y1)**2)**5
distList.append(距离)
坐标(x1,y1)

在这段代码中我还可以看到一些其他问题;例如,如果设置x1=x2,则x2-x1将始终为0查看您是否能够找出如何使用
x1=x2
行来解决此问题

欢迎来到令人敬畏的编码世界:)我们很高兴有您!尝试在
coord()
while循环中添加一些
print
语句,看看您是否能找出为什么在
step
函数中永远无法使用
break
!谢谢你的提示!,这有助于使我的输出更加清晰,但我似乎无法使if语句工作…@wavejibril在这个脚本中,您似乎根本没有调用
step()
。这可能就是为什么?
coord()
函数在
中没有
break
,而True:
block。我应该使用and而不是or吗?
x1 = 0.00
y1 = 0.00

distList = [] 

def coord(x1, y1):
    while True:
        x2 = int(input("Enter x: ")) 
        y2 = int(input("Enter y: ")) 
        x1 = x2
        y1 = y2
        distance = ((x2 - x1)**2 + (y2 - y1)**2)**.5
        distList.append(distance)

def step():
    while True:

        if(x2 == 999 or y2==999):
            break
            print(sum(distList))
        else:
            coord(x1, y1)


coord(x1, y1)