Python Forum
Identifying only specific words in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Identifying only specific words in a string
#1
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?
Reply
#2
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'])
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pulling Specifics Words/Numbers from String bigpapa 2 750 May-01-2023, 07:22 PM
Last Post: bigpapa
  Identifying if the program I have is python and then decompiling jpnyc 7 2,306 Jun-02-2022, 10:16 PM
Last Post: jpnyc
  Identifying keywords in text drchips 6 91,696 Mar-29-2022, 12:32 PM
Last Post: snippsat
  trying to put a a filter on identifying a straight CompleteNewb 1 1,656 Dec-01-2021, 11:11 PM
Last Post: CompleteNewb
  Extract a string between 2 words from a text file OscarBoots 2 1,868 Nov-02-2021, 08:50 AM
Last Post: ibreeden
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,202 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,792 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  How to extract specific key value pair from string? aditi06 0 2,520 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Replacing a words' letters in a string cananb 2 3,455 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Printing string at specific position on terminal - not showing __Mathieu__ 1 2,375 Sep-07-2020, 10:32 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020