Sal's Shipping Project Python

https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping

Hi, regarding the last checkbox, can anyone tell me why this gives:

File “script.py”, line 42

    return "The cheapest shipping method is via Ground Shipping, $ {cost_ground()}.".format(cost_ground() = cost_ground(weight))
                                                                                           ^

SyntaxError: keyword can’t be an expression

def cheapest(weight):
     if cost_premium < (cost_drone(weight) or cost_ground(weight)):
        return "The cheapest shipping method is via Premium Ground Shipping, $ {cost_premium}.".format(cost_premium = cost_premium)
     elif cost_drone(weight) < (cost_ground(weight) or cost_premium):
        return "The cheapest shipping method is via Drone Shipping, $ {cost_drone()}.".format(cost_drone() = cost_drone(weight))
     elif cost_ground(weight) < (cost_drone(weight) or cost_premium):
        return "The cheapest shipping method is via Ground Shipping, $ {cost_ground()}.".format(cost_ground() = cost_ground(weight))

print(cheapest(4.8))

print(cheapest(41.5))

here:

return "The cheapest shipping method is via Drone Shipping, $ {cost_drone()}.".format(cost_drone() = cost_drone(weight))

seems you attempt to use named placeholders:

https://pyformat.info/#named_placeholders

but the place holder should be a variable, not a function call.

Is there any way around this using the same method? Should I set a variable as a function call?

a function call is not a value
if you have a function then you can call it, and you can bind a variable to the result that you get back

in your string template you cannot have arbitrary expressions, you can only have names, names that you define somewhere else. in the place that you define the name, there you are specifying a name and an expression for the value that you want that name to refer to. you can use an expression there, and since a function call is an expression

How do I make it such that I could make it as a floating point with 2 decimal places similar to the given solution but with the new placeholder method?

.format() supports this:

https://pyformat.info/

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.