![](/img/trans.png)
[英]Why does Python's CONTINUE_LOOP allow an outer loop, when BREAK_LOOP doesn't?
[英]Why doesn't my for loop actually loop? I do not have a break function, and expected it to loop but it does not
import location
if __name__ == "__main__" :
locationlist = []
while True :
try :
coordinate = input("Enter X and Y separated by a space, or enter a non-number to stop: ")
a,b = coordinate.split()
a = int(a)
b = int(b)
locationlist.append(coordinate)
except ValueError:
break
print("Points: ",locationlist)
test = location.where_is_xy(coordinate,locationlist)
print(test)
^ 这是我的 main.py
def where_is_xy(coordinate,locationlist) :
for coordinate in locationlist :
x,y = coordinate.split()
x = int(x)
y = int(y)
if x != 0 :
if y == 0 :
Point = 'X-Axis.'
elif x > 0 :
if y > 0 :
Point = 'Quadrant 1.'
elif y < 0 :
Point = 'Quadrant 4.'
elif x < 0 :
if y > 0 :
Point = 'Quadrant 2.'
elif y < 0 :
Point = 'Quadrant 3.'
elif x == 0 :
if y != 0 :
Point = 'Y-Axis.'
elif y == 0 :
Point = 'Origin.'
return(Point)
^ 这是我的位置.py。
当输入“1 1”和“-1 -1”时,output 为“象限 1”。 我希望它通过元组列表循环回 go,但我无法弄清楚。
您在 for 循环中使用了 return 语句,因此在第一次迭代后它结束并退出循环。
def where_is_xy(coordinate,locationlist) :
for coordinate in locationlist :
x,y = coordinate.split()
x = int(x)
y = int(y)
if x != 0 :
if y == 0 :
Point = 'X-Axis.'
elif x > 0 :
if y > 0 :
Point = 'Quadrant 1.'
elif y < 0 :
Point = 'Quadrant 4.'
elif x < 0 :
if y > 0 :
Point = 'Quadrant 2.'
elif y < 0 :
Point = 'Quadrant 3.'
elif x == 0 :
if y != 0 :
Point = 'Y-Axis.'
elif y == 0 :
Point = 'Origin.'
return(Point)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.