tab compleation input
dakman at gmail.com
dakman at gmail.com
Mon Nov 13 13:18:47 EST 2006
On a gui on this would be a little bit easier but it's a completley
diffrent realm when doing it in the console. It makes it a little more
difficult when using stdin.readline() because you can only read line by
line. Here is my implmentation.
import sys
validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]
while True:
sys.stdout.write("Answer the Question: ")
answer = sys.stdin.readline().rstrip()
for valid in validanswers:
if valid.startswith(answer.strip("\t")):
answer = valid
else:
print "Invalid Answer: Please enter a valid answer"
continue
break
print "You have answered, ", answer
I'm at school and wasn't able to test it, but it looks like it should
work.
Eli Criffield wrote:
> Here is what i want to do. I have a question in my program, and i want
> to do tab completion for the valid answers.
>
> Say i have :
> --snip--
> validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]
>
> #and i ask
>
> sys.stdout.write("Answer the Question: ")
> answer = sys.stdin.readline().rstrip()
> if answer not in valid answers:
> print "Wrong!"
> --snip--
>
> But what i want is when i enter the answer i can hit tab and it'll
> complete one of the validanswers
> I know i can do tab complete with readline and 'raw_input('> ')' but
> that's only to execute python commands right?
>
> Eli
More information about the Python-list
mailing list