Python Forum
return string from function with one argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return string from function with one argument
#1
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
Reply
#2
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}')
Reply
#3
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 471 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 608 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,276 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,170 Feb-04-2023, 12:30 PM
Last Post: caslor
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,073 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,838 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,848 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,502 May-13-2022, 07:28 PM
Last Post: mikepy
  return vs. print in nested function example Mark17 4 1,736 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Regex - Pass Flags as a function argument? muzikman 6 3,576 Sep-06-2021, 03:43 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020