Skip to main content
Tweeted twitter.com/StackCodeReview/status/1124101408943431682
Became Hot Network Question
deleted 52 characters in body
Source Link
Stephen
  • 213
  • 1
  • 11

I've been taking the Computer Science course on Codecademy. The course includes this project where we have to create a python script to find the cheapest shipping method based on the weight of an item and I'm just wondering if what I wrotethis is the best way to code the solution or if there's a better one.?

The issue I see is maybe there's too many if, elif statements and I was wondering if there'smaybe there is a way to simplify the program.code

I've been taking the Computer Science course on Codecademy. The course includes this project where we have to create a python script to find the cheapest shipping method based on the weight of an item and I'm just wondering if what I wrote is the best solution or if there's a better one.

The issue I see is maybe there's too many if, elif statements and I was wondering if there's a way to simplify the program.

I've been taking the Computer Science course on Codecademy. The course includes this project where we have to create a python script to find the cheapest shipping method based on the weight of an item and I'm just wondering if this is the best way to code the solution?

The issue I see is maybe there's too many if, elif statements and maybe there is a way to simplify the code

Source Link
Stephen
  • 213
  • 1
  • 11

Find the cheapest shipping option based on item weight

I've been taking the Computer Science course on Codecademy. The course includes this project where we have to create a python script to find the cheapest shipping method based on the weight of an item and I'm just wondering if what I wrote is the best solution or if there's a better one.

def ground_shipping_cost(weight):
    flat_cost = 20
    premium_cost = 125
    if weight <= 2:
        flat_cost += weight * 1.50
    elif weight > 2 and weight <= 6:
        flat_cost += weight * 3.00
    elif weight > 6 and weight <= 10:
        flat_cost += weight * 4.00
    elif weight > 10:
        flat_cost += weight * 4.75
    return flat_cost, premium_cost


def drone_shipping_cost(weight):
    cost = 0
    if weight <= 2:
        cost = weight * 4.50
    elif weight > 2 and weight <= 6:
        cost = weight * 9.00
    elif weight > 6 and weight <= 10:
        cost = weight * 12.00
    elif weight > 10:
        cost = weight * 14.25
    return cost


def cheapest_shipping(weight):
    ground_cost, premium_cost = ground_shipping_cost(weight)
    drone_cost = drone_shipping_cost(weight)
    if drone_cost < ground_cost and drone_cost < premium_cost:
        return "You should use drone shipping as it will only cost " + str(drone_cost)
    if ground_cost < drone_cost and ground_cost < premium_cost:
        return "You should use standard ground shipping as it will only cost " + str(ground_cost)
    if premium_cost < ground_cost and premium_cost < drone_cost:
        return "You should use premium shipping as it will only cost " + str(premium_cost)


print(cheapest_shipping(4.8)) # You should use standard ground shipping as it will only cost 34.4 
print(cheapest_shipping(41.5)) # You should use premium shipping as it will only cost 125
print(cheapest_shipping(1.5)) # You should use drone shipping as it will only cost 6.75
print(cheapest_shipping(4.0)) # You should use standard ground shipping as it will only cost 32.0

The issue I see is maybe there's too many if, elif statements and I was wondering if there's a way to simplify the program.