Can I call a function by inputting a string?

I want to make a function that can be called when the text input is equal to a command.

from os import system
from time import sleep
import ctypes


print('Hi, welcome to my basic chat engine!')



username = input('Enter a username: ')

ctypes.windll.kernel32.SetConsoleTitleW('SimpleChat - ' + username)


def commands (command):
    commandlist = ['/help','/clear', '/commands']
    commanddict = {'/help' : 'help', '/clear' : 'clear', '/commands' : 'commands'}
    for possibility in commandlist:
        if command == possibilty:

def textInput (text):
    if text[0] == '/':

Does line 24 work to call a function? The way I am imagining it will work is that it will find the entry for the key 'possibility', and then call that as a function, but I am not sure.

If the previous code does not work, what would?


Suppose there's a function called help, clear,... in your code like this.

def help():

Then, the below commands function will do what you want. Note that function can be used as value of dictionary in Python.

def commands (command):
    command_dict = {'/help' : help, '/clear' : clear, '/commands' : commands}
    func = command_dict.get(command)

    if func is not None:
        print("I don't have such a command: %s" % command)

I guess '/commands''s value(command function) in command_dict should be changed to another function. The program will crash if you type 'commands'.

Need Your Help

How to change navbar collapse threshold using Twitter bootstrap-responsive?

css ruby-on-rails-3 twitter-bootstrap

I'm using Twitter Bootstrap 2.0.1 in a Rails 3.1.2 project, implemented with bootstrap-sass. I'm loading both the bootstrap.css and the bootstrap-responsive.css files, as well as the bootstrap-coll...