Python Forum

Full Version: Identifying only specific words in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new to coding and I'm trying to create a text-based adventure game as a project to help me learn. What I want is for the player to input a phrase in response to a prompt, and I want the script to detect and acknowledge only specific pre-programmed "keywords" within the phrase in order to proceed.

For example: if the pre-programmed keywords to a particular prompt are ['attack', 'shields', 'communicate'], and the player inputs "attack enemy starship", I want the 'attack' keyword to be triggered, but all the other words to be ignored.

right now what I have is this:

print("What would you like to do? ")
action = input("> ")
acceptable_actions = ['attack', 'fire', 'shields', 'defense', 'hail', 'communicate','quit']
while action.lower() not in acceptable_actions:
    print("Unknown action, try again.\n")
    action = input("> ")
if action.lower() == 'quit':
    sys.exit()
elif action.lower() in ['attack', 'fire']:
    print("You attack the alien ship!")
elif action.lower() in ['shields', 'defense']:
    print("You raise the shields!")
elif action.lower() in ['hail', 'communicate']:
    print("You try to hail the alien ship!")
and the problem is that when the player types anything but precisely the "keyword", the "Unknown action" string comes up. How can I write it so that it does what I want it to do?
Checking whether a particular string is a substring of another string is easy in Python:
>>> x = 'Japanese'
>>> y = 'pan'
>>> y in x
True
To search for a substring in list of strings, two solutions come to mind...
You can combine multiple strings in a single string (separated by e.g. space or comma), which will be searched for substring
elif action.lower() in ['attack fire'] # or ['attack, fire'], ['attack:fire']...
' '.join(['attack', 'fire') 
join combines the strings for you if you specify you strings as a list, and you can specify the delimiter (whitespace in this example).

You could also use function any() with a list comprehension, or generator object, like this:
any(action in x for x in ['attack', 'fire'])
Thanks for your help j.crater!

eventually what ended up working was this:
action = input("> ")
while True:
    if "fire" in action:
        print('You attack the alien ship!')
        break
    elif "attack" in action:
        print('You attack the alien ship!')
        break
    elif "shields" in action:
        print('You raise your shields!')
        break
    elif "defense" in action:
        print('You raise your shields!')
        break
    elif "hail" in action:
        print('You try to hail the alien ship!')
        break
    elif "communicate" in action:
        print('You try to hail the alien ship!')
        break
    else:
        print("Unknown action, try again.\n")
        action = input("> ")
There's probably a simpler way to do it, but this is the only way I could figure out right now.