Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get an if-statement to accept multiple spellings of a string?

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ...")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading