I'm working on a little demo of a Command Terminal that stores some stuff (Zones, items held within those zones, etc). I was inspired by this game called GTFO. Anyways, I'm trying to get an If Statement to accept multiple spellings of a command. For instance, the command (Help) could be full uppercase, full lowercase, grammatically correct, etc. However, while it does run up until the input, when I attempt to input Help or any variation of help, it stops dead. Tried dissecting it into a separate variable (helpCommand), but didn't work. Any suggestions?
import sys,time
def slowPrint(text,delayTime):
for character in text:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(delayTime)
#Welcome screen.
slowPrint("||Facility Main Command Terminal||\n\nEnter Help for command prompts for The Main Facitliy Command Terminal.",.05)
command = input("\n\n||Please Input Command Below||\n")
#Help Command: Gives system commands.
helpCommand = ["Help", "help", "HELP"]
if command == helpCommand:
slowPrint("\n||Commands||\n\nZoneList: Zone List lists any and all zones inside the Facility Database.\n\nZoneStock: Zone Stock shows all stocked items within specific zone.\n\nItem: After inputting command, requires the name of item. Lists where a specific item is within a specific zone and what it does.\n\nPersonal: Lists all personal in Facility Database, their work status (Working with company or not, and reason for firing) and their ID number.\n\nPersonalID: Will require an ID after command. Once ID is inputted, it will give all basic info the Personal command gives and some extra tidbits.", .05)
CodePudding user response:
If all you are looking for is case-insensitivity, you can force the case of the input to a particular setting and do the comparison that way:
if command.casefold() == 'help':
slowPrint("\n||Commands ...")
If you truly have different key words you want to match, use a set
or frozenset
to provide high-performance in
operations:
HELP_COMMANDS = frozenset(word.casefold() for word in ['help', 'sos', 'mayday'])
# ...
if command.casefold() in HELP_COMMANDS:
slowPrint("\n||Commands ...")
CodePudding user response:
You are checking if the command they give is equal to the entire list of help commands. What you are looking for is:
if command in helpCommand:
print("Commands")
CodePudding user response:
If the commands just differ in capitalization, you could change it to full uppercase or full lowercase and then just compare:
command = command.lower()
helpCommand = 'help'
if command == helpCommand:
print('commands')
Then, it won't matter the capitalization, and you can just compare directly.