0

My code seems to work perfectly except it output a float with only one decimal place instead of two as per dictionary created.

The dictionary gave each item a price e.g "Taco": 3.00; surprisingly when I run debug dictionary has different value e.g "Taco": 3.0 instead of 3.00

I would like to know why it is behaving this way.

here is my code:

# make a dictionary
dict = {
    "Baja Taco": 4.00,
    "Burrito": 7.50,
    "Bowl": 8.50,
    "Nachos": 11.00,
    "Quesadilla": 8.50,
    "Super Burrito": 8.50,
    "Super Quesadilla": 9.50,
    "Taco": 3.00,
    "Tortilla Salad": 8.00
}
# with a while loop prompt for item and add item value until user input exit
total = 0
while True:
    try:
        item = input("Item: ").strip().title()
        for order in dict:
            if order == item:
                total = total + (dict[order])
                print(f"${total}")
    except EOFError:
        break

I expected my program to output a total of two decimal places instead of just one.

Balup
  • 5
  • 5
  • 1
    Does this answer your question? [How can I format a decimal to always show 2 decimal places?](https://stackoverflow.com/questions/1995615/how-can-i-format-a-decimal-to-always-show-2-decimal-places) – G. Anderson Jan 30 '23 at 15:43
  • Thank you; but what I wanted to know is why the output Is different from the values of the dictionary? – Balup Jan 30 '23 at 15:59
  • Because you're storing a float number. Python only stores the minimum float/decimal precision necessary for any given number. `float(4.00) = 4.0` because the extra zero provides no extra information about the number. So if you want it stored with two decimals, store it as a string. If you want it stored as a number, then you have to format the output string with the extra precision you want – G. Anderson Jan 30 '23 at 16:06

0 Answers0