2013-08-21 3 views
1

어떻게 마지막 raw_input을 얻을 수 있습니까? 내 py는 질문을하고 (raw_input), 사용자가 잘못 입력하면 다시 묻고 사용자가 다시 입력해야하므로 마지막 입력을 받아서 사용자가 편집하도록하려면 어떻게해야합니까?파이썬 사용자 입력 재생

답변

1

당신은 readline module을 찾고 있습니다. 여기 example from 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(">>> ")) 
+0

예! 고맙습니다 :) – lazyy001

0

readline 모듈을 사용합니다.

import readline 

# Everything magically works now! 

탭 완성 및 기타 기능을 원하면 더 복잡한 기능을 사용할 수 있습니다.