Skip to content
Custom command for Input functions
Profile icon
[deleted]

Heya, I'm trying to make an application in Python. Do you know how I can make it so, if someone types /exit in any input, it shuts down? I already know how to make it shut down by using the os package...

import os os.system('exit')

All I need to do is add that "command" for all inputs. I just need a simpler way instead of adding "if input = "\exit" then os.system('exit')" all the time.

Any help is appreacited!

You are viewing a single comment. View All
Answered by Coder100 [earned 5 cycles]
View Answer
Profile icon
Coder100

Hello. You can put them into a dict and then loop through the keys.

Hopes this helps!

Profile icon
Coder100
def exit(): # Code here commands = { "exit": exit } # Get command command = input("command> ") # Loop through dict for key in commands: # Check if key == user input if key == command: # run the command commands[key]()

Hopes this helps!


@zinxcode

Profile icon
[deleted]

@Coder100
Thank you for answering. I'm quite new to Python, can you show me an example?

Profile icon
Coder100
Profile icon
[deleted]
Profile icon
[deleted]

@Coder100
So if a user says "exit", it will exit. Alright cool. Tysm

Profile icon
Coder100

no problem!

@zinxcode