Python用户输入回放



如何获取最后一个raw_input?我的py会问问题(raw_input),如果用户键入错误,则会再次问同样的问题,并且用户需要重新键入,那么我如何获得最后一个输入以使de user只编辑它呢??(就像外壳向上按键一样)

您正在查找readline模块。下面是effbot.org的一个例子:

# 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(">>> "))

使用readline模块。

import readline
# Everything magically works now!

如果你想要标签完成和其他好东西,还有更复杂的功能。

相关内容

  • 没有找到相关文章

最新更新