Weird outcome in sals shipping, pls help

Hi friends, Danny here.

this is my first week with python, i like it, its not easy but ill try

i was trying to make the assignement of sals shipping, en evrything went well, first you have to write a code for ground shipping, with a help from you guys i was managed to complete that area.

but in the second area the asked me to write a code for drone shipping.
i thaught i wrote my code good, cause the outcome has to be 6,75, but when i execute my code it gives me 18.

im struggeling for hours, but i dont know what to adjust to reach the outcome of 6,75.

could you friends please have a look for me?

this is the github code :slight_smile:

and this is the code:

weight = 8.4



# Ground shipping

if weight <= 2:
  price_per_pound = 1.50
if weight <= 6 :
  price_per_pound = 3.00
if weight <= 10 :
  price_per_pound = 4.00
else:
  price_per_pound = 4.75

print (price_per_pound * weight + 20)

# Ground shipping Premium

ground_shipping_premium = 125.00


print ("Ground Shipping premium " ,ground_shipping_premium)

# Drone Shipping
 
weight = 1.5 

if weight <= 2:
  cost_of_drone = 4.50
if weight <= 6 :
  cost_of_drone= 9.00
if weight <= 10 :
  cost_of_drone= 12.00
else:
  cost_of_drone = 14.25



print (cost_of_drone * weight)

if i execute this i get 18, please tell me where i did go wrong.

happy easter my friends.

Greets Danny from the Netherlands

Why do you expect 6.75? Can you provide the exercise url/link? I need to have a look at the requirements

Its assignment no 7

Thankyou for responding.

Gr Danny

The reason you’re getting 18 is because cost_of_drone = 12.00
Hence, 12 x 1.5 = 18.
Your code is running in this manner:

# Drone Shipping
 
weight = 1.5 

if weight <= 2:
  cost_of_drone = 4.50
if weight <= 6 :
  cost_of_drone= 9.00
if weight <= 10 :
  cost_of_drone= 12.00
else:
  cost_of_drone = 14.25

print (cost_of_drone * weight)

You have a series of individual “if” conditions! Meaning, each of these actually satisfy the condition when weight = 1.5 Hence, the last one weight <= 10 was met.
You should be using the if-elif-else condition!

1 Like

You should use if....elif for drone shipping. And in elif you should use and keyword. To add on: you don’t need to update the new variable to weight = 1.5 you can just change the value on line 1 of the code.

You can try to initialize cost_ground and cost_drone to be empty in a variable so you can add them into your control flow statement. That’s how I managed to do it.

Try to do it yourself first with those tips I have given:

1 Like

Thankyou, in my mind I was doing the right thing, but indeed I forgat the elif statement, how dumb.

Thankyou for helping me.

Now I can go further.

Gr Danny

1 Like

You’re welcome, feel free to mark the problem as solved.

Eugene

1 Like