I was given an assignment to give a list of possible 'weights' and I need to set a parameter for what is eligible or not

Viewed 124

this is the problem: You want to send pumpkins to some of your friends for Halloween! You look up shipping policies and find out that there are certain constraints involved when shipping packages. A pumpkin needs to weigh at least a certain amount in order to be eligible for shipping. At the same time, no pumpkin can weigh more than a certain amount due to packing restrictions.

However, you have already purchased a whole lot of pumpkins in varying sizes. You now want to find out which of these pumpkins are eligible for shipping. A pumpkin is eligible for shipping if it weighs at least as much as the minimum required weight, and not more than the maximum permitted shipping weight. You have the weights of all pumpkins in a Python list (let’s call this pumpkin_weights).

Define a function get_shippable_pumpkins that takes 3 parameters: a list of numbers named pumpkin_weights, and two floats min_weight, and max_weight. The function should compare each value in the list with the minimum and maximum weights allowed (i.e., min_weight and max_weight), and return a list of pumpkins that are within the weight limits for shipping.

You may assume that the min_weight is always less than or equal to max_weight. In other words, the lower limit of weight will always be less than or equal to the upper limit.

Implement the following function to perform the task as described. The name of your function should match exactly as shown, including cases (all lowercase) and underscores. Our autograder on gradescope will not be able to test your function if it is not named properly

so far this is all I've gotten, but im not sure what the next step would be:

pumpkin_weights = [26, 28.9, 47.3, 56.1, 60]
min_weight = 24
max_weight = 48
if pumkin_weights >= 48 and pumpkin_weights <= 26:
    pumpkin_weights = shippable
1 Answers

Simple looping with for through the list will help

def get_shippable_pumpkins(min_weight, max_weight, pumpkin_weights):
    shippable = []
    for pumpkin_weight in pumpkin_weights:
        if pumpkin_weight <= max_weight and pumpkin_weight >= min_weight:
            shippable.append(pumpkin_weight)
    return shippable


pumpkin_weights = [26, 28.9, 47.3, 56.1, 60]
min_weight = 24
max_weight = 48

print(get_shippable_pumpkins(min_weight,max_weight,pumpkin_weights))

Your result should be printed list [26, 28.9, 47.3]

Related