Please, why is "Popcorn" not replaced by "Coke" in my code?

Viewed 77

I am working on a program to get the total of items ordered in a store, for instance. I created a dictionary called menu for the available items, but if the item ordered in the list of orders is not among the key. Same of my dictionary "menu", that item should be replaced with Coke.

My intention is to convert the items to their prices, which are the corresponding values in the dictionary and do the necessary total afterwards.

But it seems, like in the example, Popcorn is not giving way.

a = ["Pizza", "Cheeseburger", "Water", "Popcorn"]
menu = {"Nachos":6,"Pizza":6,"Cheeseburger":10,"Water":4,"Coke":5}
for i in a:
   if not i in menu:
      i = "Coke"
print(a)
4 Answers

The i variable just represent the current item from the iterable, to replace the value in the array, you may use its index, enumerate gives tuple (index, element)

order = ["Pizza", "Cheeseburger", "Water", "Popcorn"]
menu = {"Nachos": 6, "Pizza": 6, "Cheeseburger": 10, "Water": 4, "Coke": 5}
for idx, item in enumerate(order):
    if item not in menu:
        order[idx] = "Coke"
print(order) # ['Pizza', 'Cheeseburger', 'Water', 'Coke']

But you'll be looking for the prices just next, you can do it in the same shot

total = 0
for item in order:
    total += menu.get(item, menu["Coke"]) # if item not present, get price of Coke
print(total)

What you are doing in your for-loop is you that you loop through the value of each element in a. Then inside your if-statement you replace the value, and not the original entry in a. Instead you have to reference the entry in a using it's index.

Try this

a=["Pizza", "Cheeseburger", "Water", "Popcorn"]
menu = {"Nachos":6,"Pizza":6,"Cheeseburger":10,"Water":4,"Coke":5}
for i in range(len(a)):
   if not a[i] in menu:
      a[i]="Coke"
print(a)

Your code does not replace Popcorn with Coke because you are not manipulating list a.

i in your code is copy of objects in list a. i does not refer to the objects in a. So, updating value of i does not update the object in the list a.

You need to manipulate objects in list a. Check code below:

a = ["Pizza", "Cheeseburger", "Water", "Popcorn"]
menu = {"Nachos":6,"Pizza":6,"Cheeseburger":10,"Water":4,"Coke":5}
for n, i in enumerate(a):
   if not i in menu:
      a[n] = "Coke"
print(a)

Result:

['Pizza', 'Cheeseburger', 'Water', 'Coke']

when you iterate over your list a using the for loop you are assigning at each iteration step to the variable i a consecutive value from a, so if you change the value of the variable i you are not changing the value from a, to change the values from a you have to access them by index:

a = ["Pizza", "Cheeseburger", "Water", "Popcorn"]
menu = {"Nachos":6,"Pizza":6,"Cheeseburger":10,"Water":4,"Coke":5}
for i in range(len(a)):
    if  a[i] not in menu:
        a[i] = "Coke"
print(a)

output:

['Pizza', 'Cheeseburger', 'Water', 'Coke']

or you could use a list comprehension:

a = [e if e in menu else 'Coke' for e in a]
# ['Pizza', 'Cheeseburger', 'Water', 'Coke']

to compute the total price you could use:

sum(map(menu.get, a))
# 25
Related