print("Hello! We have the following products in stock: ")
print(" ")
stock = [
{"Product#1": "Donuts", "Price": 17.00, "Stock:": 10},
{"Product#2": "Triki Trakets", "Price": 11.50, "Stock": 15},
{"Product#3": "Sausages", "Price": 52.00, "Stock": 7},
{"Product#4": "Cheese", "Price": 65.00, "Stock": 8},
{"Product#5": "Coke", "Price": 10.50, "Stock": 25},
{"Product#6": "Gatorade", "Price": 12.00, "Stock": 15}
]
def stonk():
x = 0
for product in stock:
for key, value in product.items():
print(key, value)
x = 1
if x % 3 == 0:
print("___")
stonk()
print(" ")
print("Prices after a 5.5% increase:")
The point of this code is to organize in a nice matter data on products in stock and their price. Now that I've arranged the data I need to update it yet I'm not sure how this is done in arrays. How can I increase "prices" by 5.5% using autonomous code?
CodePudding user response:
Since you want to increase all prices by 5.5%, all you need to do is iterate over your listNOT array!, and modify each dictionary to update its "Price"
key:
for product in stock:
product["Price"] *= 1.055
Unrelated to your original question, but your data structure doesn't quite make sense. Why append an integer to the "Product..."
key?
- You already have a list, so it makes no sense to include another index inside the item itself.
- Having a different structure of each dictionary makes it impossible to access the product name without looking at all the keys in the dictionary and finding one that starts with
"Product"
, which defeats the purpose of a dictionary.
Instead, consider renaming those keys to something uniform that describes what property of your product is specified by that value.
stock = [
{"Name": "Donuts", "Price": 17.00, "Stock": 10},
{"Name": "Triki Trakets", "Price": 11.50, "Stock": 15},
{"Name": "Sausages", "Price": 52.00, "Stock": 7},
{"Name": "Cheese", "Price": 65.00, "Stock": 8},
{"Name": "Coke", "Price": 10.50, "Stock": 25},
{"Name": "Gatorade", "Price": 12.00, "Stock": 15}
]
Now, when you access the dictionaries in your list, you can simply do item["Name"]
to get the name of each product. To format your output, you could do something like this:
print("We have the following items in stock:")
print(f"{'Product':<30s}{'Price':>6s}{'Stock':>6s}")
for item in stock:
print(f"{item['Name']:<30s}{item['Price']:>6.2f}{item['Stock']:>6d}")
which prints:
We have the following items in stock:
Product Price Stock
Donuts 17.00 10
Triki Trakets 11.50 15
Sausages 52.00 7
Cheese 65.00 8
Coke 10.50 25
Gatorade 12.00 15
See the links here and here for more information on the f-string syntax, and python's format specification syntax
If you want each product to have a "product ID", then include that as a separate key for each product.
stock = [
{"Name": "Donuts", "Price": 17.00, "Stock": 10, "id": 1001},
{"Name": "Triki Trakets", "Price": 11.50, "Stock": 15, "id": 2001},
{"Name": "Sausages", "Price": 52.00, "Stock": 7, "id": 3002},
{"Name": "Cheese", "Price": 65.00, "Stock": 8, "id": 4232},
{"Name": "Coke", "Price": 10.50, "Stock": 25, "id": 5678},
{"Name": "Gatorade", "Price": 12.00, "Stock": 15, "id": 6000}
]
Alternatively, consider changing your list stock
to a dictionary, where each key is the product ID.
stock_lookup = {item["id"]: item for item in stock}
Now, you can lookup a product by its ID in stock_lookup
:
print(stock_lookup[6000]) # {'Name': 'Gatorade', 'Price': 12.0, 'Stock': 15, 'id': 6000}
CodePudding user response:
stock = [
{"Product#1": "Donuts", "Price": 17.00, "Stock:": 10},
{"Product#2": "Triki Trakets", "Price": 11.50, "Stock": 15},
{"Product#3": "Sausages", "Price": 52.00, "Stock": 7},
{"Product#4": "Cheese", "Price": 65.00, "Stock": 8},
{"Product#5": "Coke", "Price": 10.50, "Stock": 25},
{"Product#6": "Gatorade", "Price": 12.00, "Stock": 15}
]
def stonk():
for product in stock:
new_price = ((5.5/100)*product['Price']) product['Price']
product['Price'] = new_price
You can update the price by 5.5% using above code.