Need help with Sal's Shipping assignment

I get the error: Traceback (most recent call last):
File “script.py”, line 55, in

but I have been following along with the video.

Also, one thing I do not understand about the video is how it says to use : print(“Our cheapest option available is %s shipping, it will cost $%.2f.” % (cost, method))

I don’t remember ever learning how to use “%s” and “$%.2f” in a print statement. I’m not sure how this code is even working.

Thank you! :smiley_cat:

You should provide the full error text: Just indicating a traceback from your primary function call doesn’t tell us much. However, after running your code, the error is identified as a NameError on line 40: You use a variable premium_shipping that is not defined. Ensure that you are using the correct name.


This page explains formatting Python strings (you’ll be using the “old” form).

2 Likes

Hi,
I would suggest reading your code from the bottom up. It gives you a different perspective and you might catch something w/o us just telling you what the problem is.

I will say there’s an issue with how you’re calling the function. So, you might want to re-read the lesson on that.

Let us know what you find.

2 Likes

Hello

Thank you for catching that error. But it is also showing this error:

Traceback (most recent call last):
File “script.py”, line 55, in
print_cheapest_shipping_method(4.8)
File “script.py”, line 53, in print_cheapest_shipping_method
print(“Our cheapest option available is %s shipping, it will cost $%.2f.” % (cost, method))
TypeError: must be real number, not str

You also might want to change the name of the last function. It’s not a good idea to have “print” in the function name.
def print_cheapest_shipping_method(weight):

1 Like

The thing is that I ended up following along the video provided and that Is what they did. It seemed weird, you are right.

I now there is an issue with how I am calling the function. I just don’t know what to do because in the video they were using % in the print statement and I’m not sure what that even does

this is the video: https://www.youtube.com/watch?v=46_cL0O6xyQ

Look over that last print statement inside your function. It’s not correct.

Also, the old way to format (and the new way) are in the Python documentation:

1 Like

Thank you.

I managed to fix it. The problem is I still have no idea why it works.

Im not sure how:

print(
“The cheapest option available is $%.2f with %s shipping.”
% (cost, method)
)

Does anything because in the video it says to do that, but I have not learned about this in the lessons.

1 Like

Yes, it’s not in the previous lessons and it throws learners.
(If you search the Forums, you’ll see many, many ppl who had the same question)

BUT, string formatting is in the official documentation. I highly recommend becoming familiar with the documentation. I know it can be quite dense, but, it does help and it explains everything. :slight_smile:

2 Likes

Thank you very much for your help. It seems odd that they would give me an assignment with a solution that requires outside knowledge. Would there be another way to format that string so that It would incorporate what I’ve already learned? Or were they just expecting I watch the video?

:heart_eyes_cat:

1 Like

Sure, np.

I am not sure. I can say that things will get thrown at you that are challenging and will stump you, but, you have to know where to look for answers & work through it. There’s a lot more to learn that just what’s in the lessons.
Read up on the old and new way to format b/c you might use it again.
https://www.python.org/dev/peps/pep-3101/

Happy Coding! :cowboy_hat_face:

2 Likes