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)
我一直在努力找出如何打破循环一旦用户输入999,这并没有停止循环的某些原因和另一个问题是,列表distList是为了存储旅行的距离时,用户输入的x和y值,但它覆盖了第一次输入。你知道吗
你被
coord()
函数的while循环“卡住”了:你从来没有调用过step()
,所以你从来没有在while循环中遇到过if
检查。你知道吗您的
coord()
while循环中也没有任何条件(例如if
),这意味着无法“跳出”该循环!你知道吗看看这个稍微更新的代码:
在这段代码中我还可以看到一些其他问题;例如,如果设置x1=x2,那么x2-x1将始终为0。看看你是否能想出用
x1 = x2
行来解决这个问题!相关问题 更多 >
编程相关推荐