Sals Shipping project

I never would have come close to getting this done if i wasnt able to copy line for line off the walk thru but i was able to understand the process. however, the 3rd function…

print(
“the best option is $%.2f with %s shipping”
% (cost,method))

did i cover this and forget? what is “$%.2f” and “%s”

In Python 2 that was what we had until the str.format() method was introduced.

modulo formatting uses % to indicate a placeholder.

print ("$%.2f %s" % (10.89, 'each'))
$10.89 each

https://pyformat.info/

3 Likes

It is a string formatting syntax (which it borrows from C). The %s operator lets you add a value into a Python string. In your code, the % character substitutes a value (here, with 2 decimal places).
The % operator is deprecated in favor of the more powerful str.format method, see [PEP-3101]

If you want to format that line with f-strings it would like this:

print(f"the best option is ${cost} with {method} shipping")

I hope this helps!

1 Like

i feel like we covered or used some different method for “placeholders” of values in previous lessons until the programmer covering the sals shipping project did it this way. could be wrong. this will happen when you only spend a half hour a week on something and expect to retain even a little. i don’t understand what the “s” is with the “%s”…this is not the integer but a word, whatever shipping method will be the best… couldnt a place holder just be $00.00
and how does " % = (cost,method)

im not getting this

If you followed my link above you should be clear on all your questions.

%s  =>  str type value in the argument list
%f  =>  float type value in the argument list

Would it not still require some kind of logic to fill in the values? Is that not restrictive in that it has a fixed number of whole digits?

%.2f

says to accept any float value but limit the output to 2 decimal places. The operative phrase here is, any float value.

The modulo before the argument list simply tells python that these are the values to fill in for the placeholders, in that order.


says to accept any float value

That is the same as a, directive.

2 Likes