Feb-20-2019, 10:27 PM
the best way to create a command driven execution is to use a dictionary, here's a simple example:
class SoftwareCommands: def __init__(self): self.commands = { 'func_a': self.func_a, 'func_b': self.func_b, 'willy nilly': self.willy_nilly } def func_a(self): print("\nHi, I'm func_a") def func_b(self): print("\nHi, I'm func_b") def willy_nilly(self): n = 0 while n < 10: print('n: {}'.format(n)) n += 1 def main(): command = None sc = SoftwareCommands() while command != 'quit': command = input("Enter a command, quit to end: ").strip().lower() if command in sc.commands: sc.commands[command]() if __name__ == '__main__': main()outout:
Output:Enter a command, quit to end: func_a
Hi, I'm func_a
Enter a command, quit to end: willy nilly
n: 0
n: 1
n: 2
n: 3
n: 4
n: 5
n: 6
n: 7
n: 8
n: 9
Enter a command, quit to end: quit