Use a Tuple & Loop through Scores

Darts
Darts in Python
def score(x_coord, y_coord):
    throw = x_coord**2 + y_coord**2
    rules = (1, 10), (25, 5), (100, 1), (200, 0)
    
    for distance, points in rules:
        if throw <= distance:
            return points

This approach uses a loop to iterate through the rules tuple, unpacking each (distance, points) pair (For a little more on unpacking, see Tuple Unpacking Improves Python Code Readability). If the calculated distance of the throw is less than or equal to a given distance, the score for that region is returned. A list of lists, a list of tuples, or a dictionary could be used here to the same effect:

def score(x_coord, y_coord):
    throw = x_coord**2 + y_coord**2
    rules = [[1, 10], [25, 5], [100, 1]]
    
    for distance, points in rules:
        if throw <= distance:
            return points
    
    return 0
            
#OR#

def score(x_coord, y_coord):
    throw = x_coord**2 + y_coord**2
    rules = [(1, 10), (25, 5), (100, 1), (200, 0)]
    
    for distance, points in rules:
        if throw <= distance:
            return points

#OR#

def score(x_coord, y_coord):
    throw = x_coord**2 + y_coord**2
    rules = {1: 10, 25: 5, 100: 1}
    
    for distance, points in rules.items():
        if throw <= distance:
            return points
    
    return 0

This approach would work nicely in a scenario where you expect to be adding more scoring "rings", since it is cleaner to edit the data structure than to add additional if-statements as you would have to in the if-statement approach. For the three rings as defined by the current exercise, it is a bit over-engineered to use a data structure + loop, and results in a slight (very slight) slowdown over using if-statements.

9th Apr 2025 · Found it useful?