mardi 17 août 2021

For Loop and Nested If Statement Comparison, simplification in Python

I am trying to see if there is a better way to simplify this nested if statement that I am comparing. Since each elif has it's own unique lists in them I would assume it does have to be specifically stated.

My goal is to see where a starting XY position would be that is randomly picked and whichever one is randomly selected have a set of restraining corresponding XY positions that would determine the next XY movement.

Here is what I have so far:

global_top_coords = [[5,190],
                     [97,190],
                     [188,190],
                     [279,190],
                     [370,190],
                     [462,190],
                     [553,190],
                     [644,190],
                     [750,190]]

perimeter_list= global_top_coords

random_xy_start = random.choice(perimeter_list)
for i in (perimeter_list):
    if random_xy_start == perimeter_list[0]:
        x_list = (75, 100, 125, 150)
        y_list = (75, 100, 125)
        x_rel = random.choice(x_list)
        y_rel = random.choice(y_list)
        xy_rel = (x_rel, y_rel)
        break
    elif random_xy_start == perimeter_list[1]:
        x_list = (75, 100, 125, 150)
        y_list = (75, 100, 125)
        x_rel = random.choice(x_list)
        y_rel = random.choice(y_list)
        xy_rel = (x_rel, y_rel)
        break
    elif random_xy_start == perimeter_list[2]:
        x_list = (-150, -125, -100, -75, 75, 100, 125, 150)
        y_list = (75, 100, 125)
        x_rel = random.choice(x_list)
        y_rel = random.choice(y_list)
        xy_rel = (x_rel, y_rel)
        break
    elif random_xy_start == perimeter_list[3:7]:
        x_list = (-150, -125, -100, -75, 75, 100, 125, 150)
        y_list = (75, 100, 125)
        x_rel = random.choice(x_list)
        y_rel = random.choice(y_list)
        xy_rel = (x_rel, y_rel)
        break
    elif random_xy_start == perimeter_list[7]:
        x_list = (-150, -125, -100, -75, 75)
        y_list = (75, 100, 125)
        x_rel = random.choice(x_list)
        y_rel = random.choice(y_list)
        xy_rel = (x_rel, y_rel)
        break
    elif random_xy_start == perimeter_list[8]:
        x_list = (-150, -125, -100, -75)
        y_list = (75, 100, 125)
        x_rel = random.choice(x_list)
        y_rel = random.choice(y_list)
        xy_rel = (x_rel, y_rel)
        break
xy_rel

I will be adding more to the initial perimeter list down the line but wanted to start with just the top perimeter coordinates to see how it would play out. So you could imagine I will be adding starting coordinates for the left, right, and bottom perimeter as well to the perimeter_list. This will then require a completely different set of if statement comparisons which is why I would like to see if there is a simpler way to loop through this.

Thank you for taking the time to read this!

Aucun commentaire:

Enregistrer un commentaire