How can I organize my code so that it's neater?

Question:

from subprocess import call
command = input(': ')
if command == '1':
    call('notepad.exe')
elif command == '2':
    call('calc.exe')
else:
    print('command not found')

I have similar code except it’s a lot more if statements. Main objective here is to make it take up less space / make it more organized. I am unsure of how to proceed with such task.

Asked By: justanormaluser

||

Answers:

You can e.g. create a dictionary of commands:

menu = {'1': 'notepad.exe', '2': 'calc.exe'}

Then you can use:

command = input(': ')
if command in menu:
    call(menu[command])
else:
    print('command not found')
Answered By: bb1
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.