1

So I am looking for a way to add separate items to a list in the form of individual dictionaries that contain the grocery item's name, price, and quantity. I only started programming a few weeks ago so please forgive my horrendous code and beginner mistakes.

grocery_item = dict()
current_item = dict()
grocery_history = []
choice = 0
stop = True
while stop == True:
    current_item['name'] = str(input('Item name: '))
    current_item['quantity'] = int(input('Amount purchased: '))
    current_item['cost'] = float(input('Price per item: '))
    grocery_history.append(current_item)
    choice = str(input('Press c to continue or q to quit'))
    if choice == 'c':
        stop = True
    else:
        stop = False
 print(grocery_history)

When I input the information of two items (ie spam and eggs) I get the following output:

[{'name': 'Eggs', 'quantity': 12, 'cost': 1.5}, {'name': 'Eggs', 'quantity': 
12, 'cost': 1.5}]

Instead of creating two different items the output just repeats the most recent one I entered. I am making some basic semantic error and I can't figure out how to populate the "grocery_history" list with different items from the user input loop. I tried using pythontutor.com for help but was just berated for being stupid. Any help is appreciated.

  • You always reuse the same `current_item` dict. You have to create a new one in each iteration of your loop, otherwise you overwrite the values in the old one. – Aran-Fey Apr 06 '18 at 00:45
  • You always `grocery_history.append(current_item)` but `current_item` **always refers to the same dictionary**. So you are merely appending the same dictionary to the `grocery_history` list multiple times. – juanpa.arrivillaga Apr 06 '18 at 00:46
  • How do I go about creating a new dictionary with each iteration? Can someone provide an example of how to do so? – Noobis_Maximus Apr 06 '18 at 00:48

3 Answers3

0

Try doing this:

grocery_item = dict()
grocery_history = []
choice = 0
stop = True
while stop == True:
    current_item = dict()
    current_item['name'] = str(input('Item name: '))
    current_item['quantity'] = int(input('Amount purchased: '))
    current_item['cost'] = float(input('Price per item: '))
    grocery_history.append(current_item)
    choice = str(input('Press c to continue or q to quit'))
    if choice == 'c':
        stop = True
    else:
        stop = False
print(grocery_history)

By creating a new dictionary in each loop you'll avoid the duplicate error that you're seeing.

Connor John
  • 433
  • 2
  • 8
0

You should move the current_item dictionary into the while, for example:

while True:
    current_item = {}
    # accepts user inputs here
    grocery_history.append(current_item)
    choice = str(input('Press c to continue or q to quit'))
    if choice != 'c':
        break

Some other notes:

  • No need to initiate choice = 0 before the loop.
  • Use break to stop the loop as soon as possible without going back to check the condition again
Luan Nguyen
  • 993
  • 6
  • 14
0

You're getting this because dictionaries in Python are mutable objects: this means that you can actually modify their fields without creating a brand new one.

Follow this link if you want to understand more deeply what are the main differences between mutable and immutable objects.

This is your code slightly modified, and working:

grocery_history = []
while True:
    name = input('Item name: ').strip()
    quantity = int(input('Amount purchased: '))
    cost = float(input('Price per item: '))
    current_item = {'name':name, 'quantity':quantity, 'cost':cost} 
    grocery_history.append(current_item)
    choice = str(input('Press c to continue or q to quit: '))
    if choice == 'q':
        break
print(grocery_history)
Daniele Cappuccio
  • 1,952
  • 2
  • 16
  • 31