Sal's Shipping variable and string confusion

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

When executing the code, it asked me to make the following variable into a string, as in "print("Ground: “+ str(cost_ground))” toward the end. I had put "print("Ground: “+ cost_ground”, which returned a syntax error. Is it because I asked the program to print out a string and a variable at the same time? Is there an easier way to output in this case? Thank you!

weight = 41.5

cost_ground = ""

cost_drone = ""

#Ground Shipping

if weight<=2:

cost_ground=weight*1.5+20

elif weight<=6:

cost_ground=weight*3+20

elif weight<=10:

cost_ground=weight*4+20

else:

cost_ground=weight*4.75+20

cost_ground_premium=125

#Drone Shipping

if weight<=2:

cost_drone=weight*4.5

elif weight<=6:

cost_drone=weight*9

elif weight<=10:

cost_drone=weight*12

else:

cost_drone=weight*14.25

print("Ground: "+ str(cost_ground))

print("Ground Premium: " + str(cost_ground_premium))

print("Drone: " + str(cost_drone))

Yes. It’s called, f-string. The rest is old hat and can be discarded in all your future work. Adopt the new standard and save countless hours of frustration.

If, however, that is not a luxury the lesson affords, then we have to work with what they give us. Concatenation. An awful waste of time and keystrokes, and pointless function calls to recast, and so on, is what I see it amount to. Goodness is that we can put that behind us once this lesson is over and done with.

The key concept of concatenation is making sure that everything we attempt to link into a string is itself a str object, which means re-casting non-strings.

>>> a = 2
>>> b = 3
>>> c = 4
>>> print ('y = ' + str(a) + 'x ** 2 + ' + str(b) + 'x + ' + str(c))
    y = 2x ** 2 + 3x + 4
>>> 

Now let’s look at the interpolation ability of the f-string:

>>> print (f"y = {a}x ** 2 + {b}x + {c}")
    y = 2x ** 2 + 3x + 4
>>> 

Which would you rather have to type (or read/debug) repeatedly over a long program build? One expects it will be the latter.

There are a number of directives that we can apply to interpolated expressions so you should really read up on the topic, and do an exhaustive search for write-ups so you stand a better chance to learn how to use/exploit this formatting tool.


For maths nerds, and FTR, I didn’t intend for this to be a solvable (factorable) quadratic, which it isn’t in Real terms. The 4 can only be a 1 if we’re adding. Just had to put that out there.

1 Like

Thank you so much! I’m not that far in the course yet, but now you got me excited!

1 Like

It will further excite you once you begin to play with Unicode, which is fully supported by Python in version 3.xx.

>>> a = 2
>>> b = 3
>>> c = 1
>>> print (f"f(x) = {a}x\u00b2 + {b}x + {c}\nfactors to (2x + 1)(x + 1)\nwith roots, x = -\u00bd and x = -1")
    f(x) = 2x² + 3x + 1
    factors to (2x + 1)(x + 1)
    with roots, x = -½ and x = -1
>>> 
1 Like
>>> a = 2
>>> b = 3
>>> c = 1
>>> print (f"ƒ(x) = {a}x\u00b2 + {b}x + {c}\nfactors to (2x + 1)(x + 1)\nwith roots, x = -\u00bd and x = -1")
    ƒ(x) = 2x² + 3x + 1
    factors to (2x + 1)(x + 1)
    with roots, x = -½ and x = -1
>>> 

It took awhile to figure out the small f, and even then I cheated by copy/pasting then getting Python to give me the ordinal:

>>> ord('ƒ')
402
>>> chr(402)
'ƒ'
>>> 

From that we convert to hex to derive a potential Unicode ordinal,

>>> hex(402)
    '0x192'
>>> '\u0192'
    'ƒ'

.So now we can construct that string with no tricks, at all:

>>> print (f"\u0192(x) = {a}x\u00b2 + {b}x + {c}\nfactors to (2x + 1)(x + 1)\nwith roots, x = -\u00bd and x = -1")
    ƒ(x) = 2x² + 3x + 1
    factors to (2x + 1)(x + 1)
    with roots, x = -½ and x = -1
>>> 

The only character yet to be sussed out is the italic x, so it stands out as a variable, not an operator. If you ever discover how to do that, please share. Cheers!


One last thing, if the escape syntax annoys you. just switch to triple quote string and leave out the newlines. We can write them straight into the string output.

>>> print (f'''\u0192(x) = {a}x\u00b2 + {b}x + {c}
... factors to (2x + 1)(x + 1)
... with roots, x = -\u00bd and x = -1''')
    ƒ(x) = 2x² + 3x + 1
    factors to (2x + 1)(x + 1)
    with roots, x = -½ and x = -1
>>> 

D’oh! We are still using escapes to render Unicode. All we’ve removed are the newlines.

Still tooling around,

>>> print (f'''\u0192(x) = {a}x\u00b2 + {b}x + {c}
... factors to (2x + 1)(x + 1)
... with roots, x = -\u00bd and x = -1
... vertex at, (-\u00be, -\u215b)''')
    ƒ(x) = 2x² + 3x + 1
    factors to (2x + 1)(x + 1)
    with roots, x = -½ and x = -1
    vertex at, (-¾, -⅛)
>>> 

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