Here is the project I am working on: https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-control-flow/projects/python-sals-shipping

For some reason the two methods I used to calculate drone_cost gave very different results, but not in the way I would expect. I thought it would either be very different (i.e. off by several orders of magnitude or one is negative or something) or very close (off by one decimal point or something like that). Instead they are just *kind of* different. I know it has something to do with the nitty-gritty details of the float data type but I’d like to know exactly what’s going on here. Here’s the code:

#define weight variable
weight = 1232.0000009999
"""Ground shipping section not necessary for question but necessary for code to run"""
#Ground Shipping
if 0 < weight <= 2:
cost_ground = 1.50 * weight + 20.00
elif 2 < weight <= 6:
cost_ground = 3.00 * weight + 20.00
elif 6 < weight <= 10:
cost_ground = 4.00 * weight + 20.00
elif weight > 10:
cost_ground = 4.75 * weight + 20
else:
cost_ground = "Error"
print("Ground shipping for this package will cost $" + str(cost_ground))
#commented out for forum
"""#Premium ground shipping
premium_cost = 125
print("Premium ground shipping for this package will cost $" + str(premium_cost))"""
#Drone Shipping based on ground_cost
drone_cost = 3.0 * (cost_ground - 20.00)
print("Drone shipping for this package will cost $" + str(drone_cost))
#Drone shipping based on if/elif/else statement
if 0 < weight <= 2:
drone_cost = 4.50 * weight
elif 2 < weight <= 6:
drone_cost = 9.00 * weight
elif 6 < weight <= 10:
drone_cost = 12.00 * weight
elif weight > 10:
drone_cost = 14.75 * weight
else:
drone_cost = "Error"
print("Drone shipping for this package will cost $" + str(drone_cost))

Hello!

If you think about it, the first method of calculating `drone_shipping`

is just taking the `cost_ground`

for that weight, subtracting twenty, and multiplying it by `4.75`

:

```
>>drone_cost = 3.0 * (4.75*weight+20-20)
^^^^^^^^^^^^ \
ground shipping cost this comes from the -20 in (cost_ground - 20.00)
The "20"s cancel out, so you have:
>>drone_cost = 3.0 * (4.75*weight)
Which can be written as:
>>drone_cost = 14.25 * weight
```

The other method for calculating `drone_shipping`

is simply:

```
drone_cost = 14.75 * weight
```

It is essentially (for weights over `10`

), a matter of multiplying `weight`

by either `14.25`

or `14.75`

that leads to the different numbers.

In other words, I had a typo. Oof. Thanks!

1 Like