简体   繁体   中英

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)

^ This is my 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)

^ This is my location.py.

When "1 1" and "-1 -1" is input, the output is "Quadrant 1". I would like it to loop back and go through the list of tuples, but I cannot figure that out.

You used a return statement inside the for loop so after the first iteration it ends and comes out of the loop.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM