Another approach to creating interactive applications like this is the cmd module.
# app.py from cmd import Cmd class MyCmd(Cmd): prompt = "> " def do_create(self, args): name, cost = args.rsplit(" ", 1) # args is string of input after create print('Created "{}", cost ${}'.format(name, cost)) def do_del(self, name): print('Deleted {}'.format(name)) def do_exit(self, args): raise SystemExit() if __name__ == "__main__": app = MyCmd() app.cmdloop('Enter a command to do something. eg `create name price`.')
And here is the result of running the above code (if the above code was in a file called app.py ):
$ python app.py Enter a command to do something. eg `create name price`. > create item1 10 Created "item1", cost $10 > del item1 Deleted item1 > exit $
source share