def spring():
return "It is Spring"
def summer():
return "It is Summer"
def autumn():
return "It is Autumn"
def winter():
return "It is Winter"
def default():
return "Invalid Season!"
switch_case = {
1: spring,
2: summer,
3: autumn,
4: winter
}
def switch(x):
return switch_case.get(x, default)()
y= int(input("enter a number for 4 seasons"))
print (switch(y))
I tried removing the () from the end and it doesn't return the default value anymore. When the ()
was placed right after default it didn't return any value when an input between 1 to 4 was made.
Indexing the
dictis supposed to return some function, no matter what key is presented.defaultis returned when the keyxdoesn't exist. Whatever function does get returned is immediately called.switchcould be defined more verbosely as