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.
Gaming Collection
Homepage
my-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function - Return multiple values tester_V 10 568 Jun-02-2021, 05:34 AM
Last Post: tester_V
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 547 May-02-2021, 03:45 PM
Last Post: Anldra12
  Get return value from a threaded function Reverend_Jim 3 535 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  Regular expression: return string, not list Pavel_47 3 436 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Return not exiting function?? rudihammad 3 585 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  How to use a tuple as an argument of a function zarox 5 649 Nov-14-2020, 08:02 PM
Last Post: buran
  Why does my function return None? vg100h 3 495 Oct-29-2020, 06:17 AM
Last Post: vg100h
  calling a function and argument in an input phillup7 3 609 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  Passing argument from top-level function to embedded function JaneTan 2 490 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  how to keep a Popen instance existant in a function return? Skaperen 7 764 Sep-17-2020, 07:10 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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