I am creating a python shell script that will allow the user to enter commands that output a string. But also some commands will call a function, like help, or exit I know python does not have a switch case so I am using a dictionary instead, as I do not want to use if and else statements
The problem:
The problem is I cannot use a function call inside the dictionary with the way I have it implemented as it expects to output a string, so when I enter help I get an error
#!/usr/bin/python
x = 0
while x == 0:
def helpCommand():
print "this will help you"
switcher = {
"url": " the url is: ... ",
"email": " my email is: ...",
"help": helpCommand,
"exit": exit,
}
choice = raw_input("enter your command choice: ")
def outputChoice(choice)
return switcher.get(choice)
print outputChoice(choice)
I tried to solve this problem by using function call instead, but now I get an error when trying to call a string
def outputChoice(choice)
return switcher[choice]()
TypeError: 'str' object is not callable
How to fix this problem ?