1

Does anyone know how to sum all the values, for all the items, in ‘total_price’.

My best attempt below, I got as far as getting the values but couldn’t sum them together.

It would be great if someone could tell me how to do it. (Code is condensed btw - part of a much larger, work in progress, program).

order_dictionary = {'Ham & Cheese': {'quantity': 2, 'price': 5, 'total_price': 10}, 'Hawaiian': {'quantity': 4, 'price': 5, 'total_price': 20}}

print("\n" + "*"*60 + "\nYour order:" + "\n")
for items in order_dictionary.items():
    idx, item = items
    print(f" {idx: <27} x{int(item['quantity']): <6}   ${item['total_price']:.2f}\n")
for food, info in order_dictionary.items():
    total = info['total_price']
  • Does this answer your question? [How to sum all the values in a dictionary?](https://stackoverflow.com/questions/4880960/how-to-sum-all-the-values-in-a-dictionary) – DialFrost Aug 20 '22 at 10:21
  • 1
    Welcome to Stack Overflow. "sum all the values, for all the items, in ‘total_price’." *What does this mean*? For the given input `order_dictionary`, what should the result be, and why? In your own words, step by step, what calculations need to be done in order to figure out the result? "I got as far as getting the values but couldn’t sum them together." What are "the values", in your program? How are they represented (i.e, which variables)? What is the diffficulty in adding them together? – Karl Knechtel Aug 20 '22 at 10:27
  • In your own words, where the code says `total = info['total_price']`, what is this intended to mean? What do you expect it to do, and how? What is your strategy for solving the problem? – Karl Knechtel Aug 20 '22 at 10:29
  • with a one-line list comprehension: total = sum([v["quantity"] * v["price"] for v in order_dictionary.values()]) – frab Aug 20 '22 at 10:38

3 Answers3

0

You can sum total after iterating over dictionary. You can compute total in the first loop and you don't need to write another loop. (values in the nested dict are int and you don't need to convert to int.)

order_dictionary = {'Ham & Cheese': {'quantity': 2, 'price': 5, 'total_price': 10}, 'Hawaiian': {'quantity': 4, 'price': 5, 'total_price': 20}}

total = 0
print("\n" + "*"*60 + "\nYour order:" + "\n")
for idx, item in order_dictionary.items():
    print(f" {idx: <27} {item['price']} x {item['quantity']: <6}   ${item['total_price']:.2f}\n")
    total += item['total_price']

print(f" total {' ': <34} ${total:.2f}")

************************************************************
Your order:

 Ham & Cheese                5 x 2        $10.00

 Hawaiian                    5 x 4        $20.00

 total                                    $30.00
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
-1

It looks like you have found each individual 'total_price' value. Now you just need to add them up:

total = 0
for food, info in order_dictionary.items():
    total += info['total_price']
print(total)

Output

30

quamrana
  • 37,849
  • 12
  • 53
  • 71
-1

You are almost there!

order_dictionary = {'Ham & Cheese': {'quantity': 2, 'price': 5, 'total_price': 10}, 'Hawaiian': {'quantity': 4, 'price': 5, 'total_price': 20}}

print("\n" + "*"*60 + "\nYour order:" + "\n")

# Initialise a total variable
total = 0

# This shows order recap
for items in order_dictionary.items():
    idx, item = items
    print(f" {idx: <27} x{int(item['quantity']): <6}   ${item['total_price']:.2f}\n")
    
    # Update the total during the loop
    total += int(item['quantity']) * item['price']
    
print(f"Order total: ${total}")

N.B. Please note that you actually do not need to store "total_price" as it's redundant (you can calculate it as unitary price * quantity).

decadenza
  • 2,380
  • 3
  • 18
  • 31