Reputation: 157
I have followed many examples of how to create a dictionary in lieu of a switch-case statement in Python, but for some reason, my code does not seem to be working. function ViewCommands, which should execute as it does at the beginning of the script when "viewCommands" is inputted (as command), is not being executed.
ViewCommands()
while True:
print()
print('?>> What command would you like to execute?:')
command = input()
print('>>> Executing command: ', command)
switcher = {
'viewCommands': ViewCommands,
'terminate': Terminate,
'viewSentences': ViewSentences,
'addSentences': AddSentences
}
case = switcher.get(command, '!');
if case == '!': print('!>> INVALID INPUT - NOT AN OPTION')
When I add parentheses to each of the references... like so:
ViewCommands()
while True:
print()
print('?>> What command would you like to execute?:')
command = input()
print('>>> Executing command: ', command)
switcher = {
'viewCommands': ViewCommands(),
'terminate': Terminate(),
'viewSentences': ViewSentences(),
'addSentences': AddSentences()
}
case = switcher.get(command, '!');
if case == '!': print('!>> INVALID INPUT - NOT AN OPTION')
...it does call as expected, but then I encounter a bug where Terminate is being called unexpectedly, despite how it should not have been referenced in the dictionary query.
Does anyone know why I am encountering this issue? Any advice would be appreciated, and thanks in advance!
Upvotes: 0
Views: 292
Reputation: 42143
Until Python 3.10 comes out with the match/case statement, you can define a switch function that can make the code more palatable and easier to maintain:
the switch function is this:
def switch(v): yield lambda *c: v in c
You can use it with if/elif/else ...
for case in switch(command):
if case('viewCommands') : ViewCommands()
elif case('terminate') : Terminate()
elif case('viewSentences'): ViewSentences()
elif case('addSentences') : AddSentences()
else: print("bad command")
or in a more C-like fashion ...
for case in switch(command):
if case('viewCommands') : ViewCommands() ; break
if case('terminate') : Terminate() ; break
if case('viewSentences'): ViewSentences() ; break
if case('addSentences') : AddSentences() ; break
else: print("bad command")
Upvotes: 1
Reputation: 45
This is a way to make it shorter and avoid unnecessary if statements. Lambda functions sure are beautiful aren't they?
I have added a ()
after it, as what the dictionary case statement gets is the function object, which gets executed by the () after it, same with the lambda function.
ViewCommands()
while True:
print()
print('?>> What command would you like to execute?:')
command = input()
print('>>> Executing command: ', command)
switcher = {
'viewCommands': ViewCommands,
'terminate': Terminate,
'viewSentences': ViewSentences,
'addSentences': AddSentences
}
case = switcher.get(command, lambda: print('!>> INVALID INPUT - NOT AN OPTION'))()
Upvotes: 2
Reputation: 690
In the exact moment that you close a parenthesis )
on a python function call, it will be executed, no matter where it is. This is what is happening to you in the second chunk of code: Terminate() is being executed, but you don't want it to be executed there. The code you have in the section above is the way to go. After the asignation of the case
variable, you can execute the function adding a parenthesis at the end.
while True:
print()
print('?>> What command would you like to execute?:')
command = input()
print('>>> Executing command: ', command)
switcher = {
'viewCommands': ViewCommands,
'terminate': Terminate,
'viewSentences': ViewSentences,
'addSentences': AddSentences
}
case = switcher.get(command, '!');
if case == '!':
print('!>> INVALID INPUT - NOT AN OPTION')
else:
case()
PS: I can't check the code as you haven't provided what ViewCommands, Terminate, ViewSentences and AddSentences do, or which arguments do they receive.
Upvotes: 2