I'm trying to use a for loop to generate random values for item prices, by changing the value of the item prices in a pre-defined dictionary.
The new values of this pre-defined dictionary are then added to the end of another pre-defined dictionary so a history of prices can be stored.
here is my code:
tradable_good_prices= {'iron' : 0, 'gold' : 0, 'lead' : 0, 'ruby' : 0 'emerald' : 0, 'steel' : 0, 'diamond' : 0}
item_list = tradable_good_prices.keys()
item_price_history = {}
def Random_economy(generations):
for generation_number in range(0, generations):
for list_number in range(0, len(item_list)):
tradable_good_prices[item_list[list_number]] = np.random.random_integers(100,1000)
print(tradable_good_prices)
item_price_history[generation_number] = tradable_good_prices
print(item_price_history)
Random_economy(2)
the code takes in generations as an argument for the number of for loop iterations. And using a value of 2 for generations this output is produced on the console:
{'steel': 821, 'diamond': 477, 'lead': 325, 'gold': 914, 'iron': 542, 'emerald': 360, 'ruby': 705}
{0: {'steel': 821, 'diamond': 477, 'lead': 325, 'gold': 914, 'iron': 542, 'emerald': 360, 'ruby': 705}}
{'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}
{0: {'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}, 1: {'steel': 751, 'diamond': 158, 'lead': 322, 'gold': 662, 'iron': 180, 'emerald': 846, 'ruby': 570}}
As can be seen the previous values are being overwritten, I'm guessing theres quite a simple explanation for this like "the dictionary storing the different generation values is referencing the first one for its values" but I cannot find help on this matter anywhere.
So could someone please explain to me what I'm doing wrong.