I'm a python and programming beginner and I've been trying to create a function to check whether a given key exists inside a dictionary or not and retrun boolean. This topic was helpful but did not solve my function parameter problem. I found many topics related to passing a dictionary as argument in a function but none stating how to do so with a key, couldn't find an answer to my specific problem in here.
When I use my code inside the main program, it works fine:
if "myKey" in myDict:
answ = True
print(myKey, " is there!")
else:
answ = False
print(myKey, " is not there.")
However, trying to make a function of it and then calling it doesn't work, it doesn't return an error either, just nothing happens nor gets printed.
def checkIfThere(myKey, myDict):
#for i in myDict:
if myKey in myDict:
return True
print(myKey, "is there!")
else:
return False
print(myKey, "is not there.")
That I've tried calling with the following:
checkIfThere("thisIsAKey", myDict)
checkIfThere(thisIsAKey, myDict)
checkIfThere("\"thisIsAKey\"", myDict)
What am I missing? Is it just not feasible to pass a dictionary key as argument to a function?