There's no built-in for this, but you could do something like this:
def choice(options, prompt):
while True:
output = input(prompt) # Use raw_input(prompt) for Python 2.x
if output in options:
return output
else:
print("Bad option. Options: " + ", ".join(options))
This would let the user enter in arbitrary strings (of any length) into the command line, and would continue once the user hits enter
. Unfortunately, there's no easy way that I know of to automatically stop after a set number of characters.
However, this post does discuss ways of grabbing a single character from the command line. You may be able to adapt it into the above function so that it will automatically continue after a certain number of characters, without waiting for the enter
key to be pressed.