Why is my dictionary not being changed outside of my function
Question:
Any changes that I make in products inside the function changeproducts is not being set outside of the function.
products = {
"apple": {"price": 3.5, "stock": 134},
"banana": {"price": 6.82, "stock": 52},
"cake": {"price": 23, "stock": 5}
}
def changeproducts():
#operator enter new products
items = input("Items: ").split()
prices = map(float, input("Prices: ").split())
stocks = map(int, input("Stocks: ").split())
products = {
item: {"price": price, "stock": stock}
for item, price, stock in zip(items, prices, stocks)
}
print (products)
changeproducts()
print(products)
Answers:
Python functions can’t assign to variables in global scope by default, so it would be creating a new dictionary instead. Add global products
to the top of the function:
def changeproducts():
global products
...
As Toggle said, edits within a function only exist to that function or it’s sub-functions. You can either use a global variable as suggested,
def changeproducts():
global products
...
But you can also use parameters + return values instead
def changeproducts(func_products):
#use `func_products` instead of `products`
...
return func_products
products = changeproducts(products)
print(products)
When changeproducts
is called, you give it the value of products
, which it saves as func_products
. Then once the function code has finished, it gives func_products
as it’s return value. The return value is then saved into the products
variable again
For more info, check the docs
Instead of reassigning the value, you can simply modify the list by using its update
method, so in changeproducts
do:
products.update(
{
item: {"price": price, "stock": stock}
for item, price, stock in zip(items, prices, stocks)
}
)
Any changes that I make in products inside the function changeproducts is not being set outside of the function.
products = {
"apple": {"price": 3.5, "stock": 134},
"banana": {"price": 6.82, "stock": 52},
"cake": {"price": 23, "stock": 5}
}
def changeproducts():
#operator enter new products
items = input("Items: ").split()
prices = map(float, input("Prices: ").split())
stocks = map(int, input("Stocks: ").split())
products = {
item: {"price": price, "stock": stock}
for item, price, stock in zip(items, prices, stocks)
}
print (products)
changeproducts()
print(products)
Python functions can’t assign to variables in global scope by default, so it would be creating a new dictionary instead. Add global products
to the top of the function:
def changeproducts():
global products
...
As Toggle said, edits within a function only exist to that function or it’s sub-functions. You can either use a global variable as suggested,
def changeproducts():
global products
...
But you can also use parameters + return values instead
def changeproducts(func_products):
#use `func_products` instead of `products`
...
return func_products
products = changeproducts(products)
print(products)
When changeproducts
is called, you give it the value of products
, which it saves as func_products
. Then once the function code has finished, it gives func_products
as it’s return value. The return value is then saved into the products
variable again
For more info, check the docs
Instead of reassigning the value, you can simply modify the list by using its update
method, so in changeproducts
do:
products.update(
{
item: {"price": price, "stock": stock}
for item, price, stock in zip(items, prices, stocks)
}
)