Sal's Shipping Project python

https://www.codecademy.com/paths/data-science/tracks/dspath-functions-and-logic/modules/dspath-control-flow/projects/sals-shipping

For the project above, I have 2 questions:

  1. how do I code so my print function spits out a dollar amount for numerical data in my code below?
    ie. 125.00 prints to $125.00

  2. Can someone tell me why my data is printing NONE?
    What is spits out :
    53.6
    6.75
    Ground Shipping is Cheapest

34.4
None
38.0
None
9.0
None


def GS_weight_cost(weight):
if weight > 10.0:
return (20.00 + weight * 4.75)
elif (6.0 < weight <= 10.0):
return (20.00 + weight * 4.00)
elif (2.0 < weight <= 6.0):
return (20.00 + weight * 3.00)
else:
return(20.00 + weight * 1.50)

PS = 125.00

def DS_weight_cost(weight):
if weight > 10.0:
return (weight * 14.25)
elif (6.0 < weight <= 10.0):
return (weight * 12.00)
elif (2.0 < weight <= 6.0):
return (weight * 9.00)
else:
return(weight * 4.50)

def type(weight):
if (GS_weight_cost(weight) < DS_weight_cost(weight)) and (GS_weight_cost(weight) < 125.00):
return “Ground Shipping is Cheapest”
elif (DS_weight_cost(weight) < GS_weight_cost(weight)) and (DS_weight_cost(weight) < 125.00):
return “Drone Shipping is Cheapest”
else:
return “Premium Shipping is Cheapest”

def advise(weight):
if type(weight) == “Ground Shipping is Cheapest”:
return print(GS_weight_cost(weight))
elif type(weight) == “Drone Shipping is Cheapest”:
return print(DS_weight_cost(weight))
else:
return “Premium Shipping is Cheapest”

print (GS_weight_cost(8.4))
print (DS_weight_cost(1.5))
print(type(4.8))
print( )
print(advise(4.8))
print(advise(6))
print(advise(2))

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

We can use formatting to print a character string representation…

>>> '$%.2f' % 12.345
'$12.35'
>>> '$%.2f' % 12.0000
'$12.00'
>>> 

Or using the Pythonic approach…

>>> '{:.2f}'.format(12.345)
'12.35'
>>> 

This is the answer to your other question. If you remove the print statement and just go with the return, it is printed at the caller. As it stands, the printing takes place before the return, and None is returned since print has no return.

1 Like