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. I am returning the values as f-strings, but instead of the value returned I want the key returned.
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'.
You send only the value to the function, so than you can’t get back the key:
Output:
How looks you dictionary input?
As one possible solution, you can send the whole dict:
Output:
Be aware, if resource is equal order you get None as result with your condition gt or lt, only!