If the point is within a part boundary the algorithm should return the part number. Otherwise, the algorithm should return 0.

Part n ({Xmin,Ymin},{Xmax,Ymax}).
Xmin (mm) Ymin (mm) Xmax (mm) Ymax (mm)
Part 1 30 700 180 850
Part 2 650 750 750 870
Part 3 50 20 250 120
Part 4 500 500 700 550
# Function that compares the inputs to the database variables.
def FindPart(xmin, ymin, xmax, ymax, x, y, d) :
 for k,v in d.items():
 if (x >= xmin and x <= xmax and y >= ymin and y <= ymax) :
 v = d.values()
# print(v)
 return True
 else :
 return False
# This is the database of parts
party = {"Part1": [30, 700, 180, 850],
 "Part2": [650, 750, 750, 870],
 "Part3": [50, 20, 250, 120],
 "Part4": [500, 500, 700, 550]}
# Input of parts search
x = int(input('Enter a number: '))
y = int(input('Enter a number: '))
d = party
key_list = list(party.keys())
val_list = list(party.values())
# Loop through the parts bin database and compare inputs
# to find the exact bin
for xmin , ymin , xmax , ymax in party.values():
# Function call
 if FindPart(xmin, ymin, xmax, ymax, x, y, d):
 w = [i for i, e in enumerate(w) if FindPart(xmin, ymin, xmax, ymax, x, y, d)]
 print(w)
 break
 else:
 print("0")`
3

There are 3 best solutions below

1
not_speshal On

You don't need to loop over your dictionary twice. Either do it in the function or in your main code. Try:

def withinBoundary(xmin, ymin, xmax, ymax, x, y):
    if x in range(xmin, xmax+1) and y in range(ymin, ymax+1):
        return True
    return False

# Input of parts search
x = int(input('Enter a number: '))
y = int(input('Enter a number: '))

matches = list()
for part, limits in party.items():
    if withinBoundary(*limits, x, y):
        matches.append(part)
print(matches)
0
Paul On

Here is a slight modification to not_speshal's answer:

def within_boundary(xmin, ymin, xmax, ymax, x, y):
    if xmin <= x <= xmax and ymin <= y <= ymax:
        return True
    return

parts = {
    "Part1": [30, 700, 180, 850],
    "Part2": [650, 750, 750, 870],
    "Part3": [50, 20, 250, 120],
    "Part4": [500, 500, 700, 550]
}

# Input of parts search
x = int(input('Enter a number: '))
y = int(input('Enter a number: '))

for part_number, limits in enumerate(parts.values(), start=1):
    if within_boundary(*limits, x, y):
        boundary = part_number
        break
else:
    boundary = 0

From your code and question it looks like you would like to identify the single region that the point belongs to, rather than a list of regions.

The above for loop will break if the point is within a given region. The else block will only be executed if the for loop iterates over all elements without breaking - i.e. if the point is not within any region.

Edit - I realised you want the part number not the part name. I've updated the for loop to reflect this.

0
apemaharaja On
def within_boundary(xmin, ymin, xmax, ymax, x, y):
    if xmin <= x <= xmax and ymin <= y <= ymax:
        return True
    return

parts = {
    "Part1": [30, 700, 180, 850],
    "Part2": [650, 750, 750, 870],
    "Part3": [50, 20, 250, 120],
    "Part4": [500, 500, 700, 550]
}

# Input of parts search
x = 650 #int(input('Enter a number: '))
y = 751 #int(input('Enter a number: '))

for part_number, limits in enumerate(parts.values(), start=1):
    if within_boundary(*limits, x, y):
        boundary = part_number
        break
else:
    boundary = 0
    
print("Part", boundary)