Python user input replay
本问题已经有最佳答案,请猛点这里访问。
我怎样才能获得最后的raw_input? 我的py询问问题(raw_input),如果用户类型错误再次询问相同,并且用户需要再次输入,那么如何让最后一个输入让de用户只需编辑它?(就像一个shell按下键盘)
您正在寻找
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # File: readline-example-2.py class Completer: def __init__(self, words): self.words = words self.prefix = None def complete(self, prefix, index): if prefix != self.prefix: # we have a new prefix! # find all words that start with this prefix self.matching_words = [ w for w in self.words if w.startswith(prefix) ] self.prefix = prefix try: return self.matching_words[index] except IndexError: return None import readline # a set of more or less interesting words words ="perl","pyjamas","python","pythagoras" completer = Completer(words) readline.parse_and_bind("tab: complete") readline.set_completer(completer.complete) # try it out! while 1: print repr(raw_input(">>>")) |
使用
1 2 3 | import readline # Everything magically works now! |
如果您想要标签完成和其他好东西,还有更复杂的功能。