0

I am trying to execute the problem "Felipe´s Taqueria", but I am failing to see any price printed as output. Instead the program keeps asking for input.

When I input "Taco", the output I get is "Item : ". And again, and again, and again... What is wrong?

I changed the indentation as follows, but I have the same issue as before:


food_price = { "Baja Taco": 4.25, "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 }
   
while True:
    try:
        x = (input("Item : "))
        for key, value in food_price.items():
            if key == x:
                x = value
                print(f"{x} $")
                x = x + x
                print(f"{x} $")
            else:
                x = (input("Item : "))
    except EOFError:
        break
2
  • 1
    The indentation in the posted code gives error and program does not run. Commented Mar 9 at 16:37
  • Many thanks for the pointer. I tried to review the indentation, I added the changed code, however this is leading to the same result. Commented Mar 9 at 16:54

2 Answers 2

1

There are three problems, though the second two are kind of redundant with one another. But since you are learning you should go through both of those fixes. I wouldn't have bothered with my answer if this wasn't such an egregious misuse of a dictionary, if this was a list I would have helped Manford fix his answer to be more complete as he is nearly there with issue 2 but his explanation is lacking.

  1. you are capturing the result of your input statement as a tuple instead of as a scalar. Remove the parenthesis that are surrouding input("Item: ") it should just be x = input("Item: ").

  2. You need a break in your for loop whenever you find a match. You need to eliminate that else statement on the if/else there. If you don't remove the else anytime your input x doesn't match the first thing in the dictionary ( which is what you are iterating through in the for loop) you will ask for input again but continue searching through the list from where you are which is terribly wrong. You need to try all the matches in the loop until you find a match or exhaust your items before you change your input... But this whole line of logic is horribly horribly inappropriate. You should fix it to understand how logic flows in using loops, but you need to then move on to item 3.

  3. It doesn't make any sense to use a loop like this at all. food_price is a dictionary (key value pairs). The whole point of a dictionary is that you can search for it efficiently without having to iterate over it like a list. Get rid of the loop iterating through food_price.items() and just check if the input food item is a member of the object and if so get its value.

cost = 0

# possibly a loop structure in which you get user input and do work
while True:
    #read input
    try:
        x = input("Item: ")

        if x in food_price: # is the value held by x a key to the dictionary?
            cost = cost + food_price[x] # yes so get the price of that item and add it
        else:
            # whatever you want to do if the item isn't in the food_list dictionary

    except EOFError:
        break

print("Total cost is: ", str(cost))

7
  • OK, many thanks this work. I am sorry for this stupid remark but you said "egregious misuse of a dictionary"... how do you know how to use things correctly? I mean for me it is far from intuitive... I am sorry for the tuple, this was a mistake. Something that remained from an old code that I had wrote to lower the characters inputted by the user. Commented Mar 10 at 21:42
  • 1
    actually watch the lessons from the course dont just ignore the lessons and try to solve the problems.. read documentation right in python's own site docs.python.org/3/tutorial/datastructures.html#dictionaries
    – UpAndAdam
    Commented Mar 11 at 15:34
  • 1
    You dont have to be sorry for any of it, especially the tuple; it's a mistake. How to know what to do? You have to watch the lectures, and I'd suggest consulting python.org for documentation if you don't find an answer directly in the lecture and the notes/slides sample code etc. If you dont gain an understanding of WHY you are doing the WHAT is meaningless.
    – UpAndAdam
    Commented Mar 11 at 15:37
  • Yes...I really agree....changing something without understanding is absurd...this is a bit what worries sometimes. I feel that I do not understand enough or at least would not be able to apply it to a similar situation. I always look at the course, when doing so it seems super simple, but the exercise feel harder. Regarding the documentation of python, I tried reading the doc, and I have an enormous amount of difficulty understanding it sometimes. I feel it is not the clearest... W3School seemed sometimes clearer but I have been told it was not so great in terms of correctness sometimes... Commented Mar 11 at 23:18
  • 1
    what do you mean by third function?
    – UpAndAdam
    Commented Mar 28 at 0:35
1

I think the issue is with your else statement inside of your for loop. The for loop is iterating through all of the items in your list until it finds a match to your input. So the first iteration, your if statement is comparing "Baja Taco" to your input, if statement is False, so you're prompted to input again. Then on the next iteration, you're if statement is comparing "Burrito" to your input, if statement is False, so you're prompted again, and so on...

I don't think you need the else statement. Once you've made a match, you can put a break in your if statement to exit out of the for loop.

1
  • lacks explanation of why prompting again is so so so bad. but very much on the right trail and solves the problem
    – UpAndAdam
    Commented Mar 13 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .