Aug-06-2018, 05:15 AM
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:
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?