I'm trying to make a dictionary which contains functions as the values. The idea is that the difficulty variable will change throughout the program, and this will affect which function we call from the dictionary. For the sake of this, I'm not going to show the program, I've made a simple mock up that demonstrates the issue I'm having.
Whenever I run the code, without even entering a key, it runs the functions. Why is this?
def printhello():
print("hello")
def printgoaway():
print("go away")
x={1:printhello(),2:printgoaway()}
At this point I wasn't expecting anything to have happened as I haven't called any keys yet. It runs the functions anyway and prints the values.
If I then call them by doing x[1]
or x[2]
, nothing happens.
Can someone explain to me how I would use functions in a dictionary, and stop them from automatically calling them when I make the dictionary