Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary Problem
#1
I'm trying to write a program that keeps a running total of a "customer's" total as they input items off a menu, which in this case happens to be my dictionary with items and costs assigned to one another. The total continues until the "customer" hits ctrl + d, which ends the program.

The issue I appear to be having is with items in the dictionary having similar names. The problem kids are "Super Burrito: 8.50", "Burrito: 7.50", "Super Quesdilla: 9.50", and "Quesadilla: 8.50" The numbers are their individual costs and for some reason, super burrito and and super quesadilla are trying to register as "super 7.5" and "super 8.5", respectively, which the system won't accept. Baja taco and taco do not seem to be an issue, so it is obviously the word "super" but I don't understand why that's a problem or how to fix it.

I'm a beginner in coding, but if anyone could please point me in the right direction or tell me which line of code is creating the problem, that would be much appreciated. Thank you!

menu = {
    "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
}

total = 0.00
while True:
    try:
        order = input("Item: ").lower()
        if order in menu:
            for item, cost in menu.items():
                cost = str(cost)
                order = order.replace(item, cost)
            total += float(order.strip("super"))
            print(f"Total: ${total:.2f}")
    except EOFError:
        print ("\n")
        break
Reply
#2
If you want to get the cost of an item.
item_cost = menu[item]
That is it. No looping. No looking at items(items). No converting the "cost" to a string, stripping out "super" and converting back to a float.

You should inform the user when the enter an item that is not on the menu.
if item in menu:
    # add item cost to total
else:
    print(f"{item} is not on the menu.")
Reply
#3
(Apr-23-2023, 02:15 AM)deanhystad Wrote: If you want to get the cost of an item.
item_cost = menu[item]
That is it. No looping. No looking at items. No converting the "cost" to a string, stripping out "super" and converting back to a float.

Yeah, the whole strip("super") was an admittedly far-fetched attempt to work around the issue, but I see what you're saying. Like I said, I'm very new to all of this (coding in general, not just python) so I think I'm just over-complicating everything.

But I was able to take what you said and it now works as it should, so thank you for your help!
Reply
#4
As an aside, on my Windows 11 computer with Python 3.11, ctrl + d does not raise an EOFError. The only way I can exit your program is to kill it or ctrl + c (keyboard interrupt). If I was writing this, I would break the loop when a blank item is entered (just enter).
Reply
#5
my_dict = {"foo": 42}

value = my_dict.get("does not exist")
# now value is None
# the method get returns by default a None if the key is missing.
As a function:
def buy(menu):
    print("q/Q to stop the order", end="\n\n")
    total = 0.00
    while True:
        # expecting that all items are in lower case
        # in the menu
        order = input("Item: ").lower().strip()
        if order.lower().strip() == "q":
            break

        price = menu.get(order)
        if price is None:
            print("Item is not in list", end="\n\n")
            continue

        total += price
    return total
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020