Python Forum

Full Version: return string from function with one argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def ground_shipping_cost(weight):
  cost = weight * 4.00
  return cost

#print(ground_shipping_cost (8.400)+20)

def drone_shipping_cost(weight):
  cost = weight * 4.50
  return cost

#print(drone_shipping_cost (1.5)) # static

def method_is_cheapest(weight):

  ground_ship = ground_shipping_cost (weight)
  drone_ship = drone_shipping_cost (weight)
  premium_ground_price = 125.00 # static

  if (ground_ship < drone_ship) and (ground_ship < premium_ground_price): # ground_ship is cheapest
    return ground_ship
  
  elif (drone_ship < ground_ship) and (drone_ship < premium_ground_price): # drone_ship
    return drone_ship
  
  elif (premium_ground_price < ground_ship) and (premium_ground_price < drone_ship): #premium_ground_price
    return premium_ship

"""
def method_price(weight):

  return price
"""

step_6 = method_is_cheapest(4.8)
    
print_string = "Ship price: " + str(step_6) + "Cheapest ship method: " + str(ship_method)"
print(print_string)

method_is_cheapest(100)
Hello all, i would like str(ship_method) to include a string from the return values (ground_ship, drone_ship, or premium_ship), whatever value is returned smallest from the calculation. I am very new to python coming from C
The solution is the same solution I would use for solving this with C, create an association between the shipping cost function and the shipping cost name. In Python I might use a dictionary.
def ground_shipping_cost(weight):
  cost = weight * 4.00
  return cost
 
def drone_shipping_cost(weight):
  cost = weight * 4.50
  return cost

def premium_ground_cost(weight):
    return 125

shipping_options = {
    'Ground': ground_shipping_cost,
    'Drone': drone_shipping_cost,
    'Premium Ground' : premium_ground_cost
    }

def method_is_cheapest(weight):
    option = None
    min_cost = 0
    for name in shipping_options:
        cost = shipping_options[name](weight)
        if cost < min_cost or option is None:
            min_cost = cost
            option = name
    return option

weight = float(input('Enter package weight: '))
option = method_is_cheapest(weight)
cost = shipping_options[option](weight)
print(f'Shipping {option} is the cheapest at ${cost:.2f}')
If your wanting to add a string to the return, you can do this
#! /usr/bin/env python3
def ground_shipping_cost(weight):
  cost = weight * 4.00
  return cost

#print(ground_shipping_cost (8.400)+20)

def drone_shipping_cost(weight):
  cost = weight * 4.50
  return cost

#print(drone_shipping_cost (1.5)) # static
def method_is_cheapest(weight):

  ground_ship = ground_shipping_cost (weight)
  drone_ship = drone_shipping_cost (weight)
  premium_ground_price = 125.00 # static

  if (ground_ship < drone_ship) and (ground_ship < premium_ground_price): # ground_ship is cheapest
    return ground_ship, 'Ground Shipping' # Add string to return

  elif (drone_ship < ground_ship) and (drone_ship < premium_ground_price): # drone_ship
    return drone_ship, 'Drone Shipping' # Add string to return

  elif (premium_ground_price < ground_ship) and (premium_ground_price < drone_ship): #premium_ground_price
    return premium_ship, 'Premium Shipping' # Add string to return


step_6 = method_is_cheapest(4.8)
#access the return index
print_string = f'Ship Price: {step_6[0]} Cheapest ship method: {step_6[1]}'
print(print_string)
Output:
Ship Price: 19.2 Cheapest ship method: Ground Shipping