1

如何获得最后一个 raw_input?我的 py 提出问题(raw_input),如果用户输入错误,则再次询问相同的问题,并且用户需要重新输入,那么我怎样才能获得最后一个输入以使 de 用户只需编辑它??(就像一个外壳按下键)

4

2 回答 2

1

您正在寻找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(">>> "))
于 2013-08-21T23:24:08.677 回答
0

使用readline模块。

import readline

# Everything magically works now!

如果您想要标签完成和其他好东西,可以使用更复杂的功能。

于 2013-08-21T23:29:07.667 回答