
Kunal S. answered 09/21/21
Software developer with a passion for teaching
I'm going to try to break this problem into pieces (note that this is just one of many potential valid solutions!)
Determining the cost for 1oz based on the type of input
We can create a conditional statement using "if" and "elif" to determine whether the item is a "letter", or a "large" letter. Depending on which one it is, we can assign an appropriate value to the "cost_for_1_oz" variable. See if you can fill in the "???"
Handling the case in which item is invalid
Per the instructions, we must return -1 if the item is not "large" or "letter". We can modify our conditional slightly to do this, by adding an "else" statement. Note that "else" acts as a "catch-all", and will execute only if the previous if/elif statements fail. Also note that the "return" statement will immediately exit the function upon execution, ignoring any code below.
Determine the cost for additional ounces
We know that the cost for a single ounce is stored in our variable "cost_for_1_oz". Let's create a variable to store the cost for additional ounces. The equation might look something like below (see if you can fill in the blanks). Note how we calculate additional ounces as the weight, minus the first ounce. Also note that this code works *assuming* ounces is always a whole number.
Performing the cost calculation for the item
We now know the cost for the first ounce, and the cost of all additional ounces. What do you think we need to do to calculate the total cost?