PHP Code:
kill = ["kill", "kilL", "kiLl", "kiLL", "kIll", "kIlL", "kILl", "kILL", "Kill", "KilL", "KiLl", "KiLL", "KIll", "KIlL", "KILl", "KILL"]
for form in kill:
if k == form:
K = input ("do you want to abort? Y/N")
if K == "Y":
It sometimes surprises me that people reach to a certain solution, that it's complex, and never question if there's an easier way. In this case, instead of trying every possible combination to match the input, why not convert the input to a known form?
PHP Code:
query = input("What do you want to do:")
if query.lower().strip() == "kill":
query = input("Do you want to abort?")
Matching the lowercase covers all possibilites (And strip in case of random spaces like "kill "). Also, instead of "aborting" everything, why not wrap it in another way, so that the function already performs this action. Also, read on dictioanries, lists, and the array module. This are basic data structures that can improve the performance/rediability (and therefore, scalability) of your code.
Here's a simple example of having a dictionary that holds references to functions:
PHP Code:
#!/usr/bin/env python3
import sys
def showhelp():
"""Shows help to the user"""
print("""--------- help ---------
Use either quit or exit to leave the application
blah blah, more help""")
def close():
"""Closes the application"""
print("Shutting down")
sys.exit()
def request(question):
"""Asks a user a certain question, that can be escaped with a command"""
response = input(question).lower().strip()
# Try to see if it's a known command, retrieve the function and perform it.
if response in actions:
actions[response]()
return response
actions = \
{
"help":showhelp,
"quit":close,
"exit":close
}
# Example usage
request("Do you like noodles?")
It uses more clear variable names, wraps the common task (checking if user types a certain command) inside a function, so you avoid repetition, etc.
You should really work harder the code, and your variable names. Have fun.
Bookmarks