EDIT: Figured out a way to do it. Instead of passing in the Key to get the value, I pass in the entire dictionary and loop through both key and value.
for key, value in current_resource.items():
if order > value:
return f"Sorry there is not enough {key}"
elif current_resource > order:
return f"Okay, here is your order."
print(check_resources(MENU["espresso"]["ingredients"]["water"], current_inventory))
=============
I’ve created a function that requests two parameters and the second (current_resource) is taking in a value from an out of scope dictionary and comparing the value against a user request ex: order. I am returning the values as f-strings, but instead of the value returned I want the key returned in the F-string.
The function:
def check_resources(order, current_resource):
"""Checks if resources are greater than the items requirements. If so, returns order. Or Returns missing req."""
if order > current_resource:
return f"Sorry there is not enough {current_resource}"
elif current_resource > order:
return f"Okay, here is your order."
The requesting code:
print(check_resources(MENU["espresso"]["ingredients"]["water"], current_inventory["water"]))
It is checking a dictionary where I have the keys and values stored, in this case above, I am comparing it to the value of “water”. And when returned, it returns an INT in the form of the value.
I am in search for way to return the Key name in the F-string.
I have tried using the .key() method such as:
current_resource.key()
but it returns the error:
AttributeError: ‘int’ object has no attribute ‘key’.
Which makes sense as just the value is passed in. Is there a way to have it “step back” and pass the key instead?