Python 3 1st project question


I just finished the 1st project in the Python 3 course, and I have a question regarding the code. So, I was able to get a correct(ish) final number, but instead of representing a real dollar amount, it was several decimal places long. In this case, 333.09119999999996, instead of 333.09. I did some research on stack overflow and got my answer. But that led to other questions.

1st project:

First question, did I do the project correctly? I feel like that is an advanced solution to the decimal formatting. Maybe I overlooked something? Please ignore the extra formatting. I wanted to make it look a little more like a receipt. Im referring to the commented-out line at the bottom of the program. With the line commented out, the result is $333.09119999999996. If I remove the #, the answer is $333.09.

Here is my code:
lovely_loveseat_description = “Lovely Loveseat: Tufted polyester blend on wood. 32 inches high x 40 inches wide x 30 inches deep. Red or white.”
lovely_loveseat_price = 254.00

stylish_settee_description = “Stylish Settee: Faux leather on birch. 29.50 inches high x 54.75 inches wide x 28 inches deep. Black.”
stylish_settee_price = 180.50

luxurious_lamp_description = “Luxurious Lamp: Glass and iron. 36 inches tall. Brown with cream shade.”
luxurious_lamp_price = 52.15

sales_tax = .088

customer_one_total = 0

customer_one_itemization = “”

customer_one_total += lovely_loveseat_price

customer_one_total += luxurious_lamp_price

customer_one_itemization += lovely_loveseat_description + luxurious_lamp_description

customer_one_total_tax = customer_one_total * sales_tax

customer_one_actual_total = customer_one_total + customer_one_total_tax

print(“Customer One Items: “)
print(”…” + “" + str(lovely_loveseat_price)) print(luxurious_lamp_description) print("..." + "” + str(luxurious_lamp_price))
print("Tax = " + str(sales_tax))
print(“Customer One Total: “)
#customer_one_actual_total = “%.2f” % customer_one_actual_total
print(”$”+ str(customer_one_actual_total))

Second question, what does that code even mean/do?

customer_one_actual_total = “%.2f” % customer_one_actual_total

Im referring to the “%.2f” part, not the entire line. Why isnt that part being processed as a string, since it has the quotes?

Thanks :slight_smile:

This tells the formatter to expect a float and to give it two decimal places.

The intepreter recognizes the % formatting operator and its arguments.

% [decimal point] [number of decimal places] [type]

The string could be any length, but the quotes are imperative else the interpreter won’t recognize the placeholder.

"Your total is, %.2f" % customer_one_actual_total


Thanks for your quick response!

1 Like