Yes! There are other ways to figure out the additional weight and charges for cases where the weight exceeds 100g. The method I provided earlier (using // for integer division and % for leftover weights) works well, but we can approach the calculation differently while maintaining efficiency and readability.
Below is an alternate approach that recalculates the additional weight and charges without explicitly using division operators.
Alternate Implementation:
def shipping_fee(weight):
"""
Calculate the cost of mailing a package based on its weight.
Parameters:
weight (float): The weight of the package in grams.
Returns:
str: A string indicating the cost of shipping, formatted as currency.
"""
if weight <= 30:
cost = 0.50
elif weight <= 50:
cost = 0.65
elif weight <= 100:
cost = 0.70
else:
# For weights above 100g, calculate each additional 50g increment
additional_weight = weight - 100
additional_charges = 0.0
# Loop to add $0.25 for every 50g increment
while additional_weight > 0:
additional_charges += 0.25
additional_weight -= 50
# Base cost ($0.70 for first 100g) + additional charges
cost = 0.70 + additional_charges
return f"The cost of shipping is: ${cost:.2f}"
# Main Program to Test Your Function
weight = float(input("Enter the mass: "))
result = shipping_fee(weight) # Call the function
print(result)
Explanation of the Alternate Approach:
-
Handling Additional Weight with a Loop:
- Instead of using
// or % directly to compute how many 50g increments there are, we loop through the additional weight, deducting 50g in each iteration.
- For every 50g deducted, we add $0.25 to
additional_charges.
- Once the additional weight is less than or equal to 0, the loop stops.
-
Combining Costs:
- After calculating the additional charges with the loop, we combine them with the base cost of $0.70 for packages up to and including 100g.
-
Formatted Return String:
- The function still formats the total cost to two decimal places for a clean output.
How It Works for Different Inputs:
Example 1: Weight = 35g
Input:
Enter the mass: 35
Output:
The cost of shipping is: $0.65
- Weight ≤ 50g → cost is $0.65.
Example 2: Weight = 120g
Input:
Enter the mass: 120
Output:
The cost of shipping is: $0.95
Calculation:
- Base cost for the first 100g: $0.70.
- Additional weight = 20g.
- Loop runs once for the first 50g increment (20g is less than or equal to 50g):
- Add $0.25 for this increment.
- Total cost = 0.70+0.25 = $0.95.
Example 3: Weight = 180g
Input:
Enter the mass: 180
Output:
The cost of shipping is: $1.20
Calculation:
- Base cost for the first 100g: $0.70.
- Additional weight = 80g.
- Loop runs twice:
- Deduct 50g → Add $0.25 (first increment).
- Deduct 50g → Add $0.25 (second increment).
- Total cost = 0.70+0.25 + 0.25=1.20.
Why Use This Approach?
- Readable: The
while loop is easier to follow as it clearly demonstrates what happens for every additional 50g increment.
- Avoids Integer Division: Instead of using mathematical operators like
// or %, this approach uses simple subtraction and repetition.
- More Flexible: If future requirements add different logic for increments (e.g., more charges for every 25g), this approach can accommodate that more easily with minor changes.
Alternative with Fewer Lines (But Still Using while):
For those who prefer even fewer lines while using similar logic, here's a more concise version:
def shipping_fee(weight):
if weight <= 30:
cost = 0.50
elif weight <= 50:
cost = 0.65
elif weight <= 100:
cost = 0.70
else:
additional_weight = weight - 100
cost = 0.70
while additional_weight > 0:
cost += 0.25
additional_weight -= 50
return f"The cost of shipping is: ${cost:.2f}"
Here, the cost calculation directly incorporates the additional charges within the loop. This avoids creating a separate variable like additional_charges.
Both versions (loop-based and the original // + %-based approach) are valid, and the choice depends on your preferred coding style and the specific requirements.
Let me know if you have further questions! 😊