So I’m working on a text based game in which I want the user to type their next action in. I’m trying to work out a way to code it so that it may be less “picky”. I want the game to accept partial input (for example n instead of north), also I want it to ignore prefixes such as “go”.
I’ve worked out the partial input with a for-loop, it also accepts input with the “go” prefix.
However, if I simply type “go” without entering a direction it defaults to “north”, which is the first part of my list of valid commands. This problem also appears when giving empty input.
What I’m looking for now is a way to let the prefix vary, with something like “check” in front of map or “walk” in front of south. I also need to make it recognize when the input only contains a prefix and not an actual command.
This is the relevant code at the moment.
move_input = input("You silently ask yourself what to do next.\n").lower()
for n in valid_moves:
if n.startswith(move_input) or ("go " + n).startswith(move_input):
move_input = n
You can try splitting it to words:
If you want to have different prefixes for different commands, you can have something like:
But it is starting to look too complex.