本文介绍了Python 输入提示返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个 raw_input 语句的脚本.我想添加输入撤消"的功能,并允许用户返回并重新输入最后一个 raw_input 值.如果没有 goto 类型语句,我如何完成此操作?

I have a script which has multiple raw_input statements. I would like to add the ability to enter 'undo' and allow the user to go back and re-enter the last raw_input value. How can I accomplish this without a goto type statement?

import sys

prompt_a = raw_input('Please enter x for x \nPlease enter y for y \nPlease enter c to cancel: ') or 'x'
prompt_a = prompt_a.lower()

if prompt_a == 'c':
    sys.exit()


prompt_b = raw_input('Please enter x for x \nPlease enter y for y \nPlease enter c to cancel \nPlease enter u to go back to previous question: ') or 'x'

if prompt_b == 'c':
    sys.exit()

#I would like to GOTO prompt a if the user entered u in the last prompt

prompt_c = raw_input('Please enter x for x \nPlease enter y for y \nPlease enter c to cancel \nPlease enter u to go back to previous question: ') or 'x'

if prompt_c == 'c':
    sys.exit()

#I would like to GOTO prompt b if the user entered u in the last prompt

更新

def function1(user_input):
    var_a = user_input.lower()
def function2(user_input):
    var_b = user_input.lower()
def function3(user_input):
    var_c = user_input.lower()

processing_functions = [function1, function2, function3]
progress = 0

while progress < len(processing_functions):
    user_input = raw_input("Enter input please: ")
    if user_input == "UNDO":
        progress -= 1
        print("Returning to previous input.")
    else:
        print("Processing " + user_input)
        progress += 1
        if progress == 1:
            function1(user_input)
        elif progress == 2:
            function2(user_input)
        elif progress == 3:
            function3(user_input)



print("Processing complete.")
print(var_a)
print(var_b)
print(var_c)

推荐答案

想到的第一种方法是将您对每个输入的操作分离到一个函数中.这样你就可以建立一个这些功能的列表,并逐一进行.这意味着如果用户输入撤消输入,您可以后退一步并转到上一个功能.

The first way that comes to mind is to separate what you do with each input into a function. That way you can build a list of these functions and progress through them one by one. This means that if the user enters the undo input, you can take one step back and go to the previous function.

def function1(user_input):
    # do something
def function2(user_input):
    # do something
def function3(user_input):
    # do something

processing_functions = [function1, function2, function3]
progress = 0

以上是设置.每个函数都可以接受用户的输入并对其进行任何需要的操作.如果您不熟悉,列表也会引用函数.您可以通过访问索引然后使用括号从列表中调用函数,例如.processing_functions[0](user_input).

The above is the set up. Each function can take the user's input and do whatever it needs to with it. In case you're not familiar, lists take references to functions too. And you can call a function from a list just by accessing the index and then using the parentheses, eg. processing_functions[0](user_input).

现在我们将如何进行实际循环:

So now here's how we'd do the actual loop:

while progress < len(processing_functions):
    user_input = raw_input("Enter input please.")
    if user_input == "UNDO":
        progress -= 1
        print("Returning to previous input.")
    else:
        print("Processing " + user_input)
        processing_functions[progress](user_input)
        progress += 1

print("Processing complete.")

当您还有函数要做(进度小于函数数)时,您继续运行循环,并继续调用每个函数.除了,如果使用了撤消命令,则进度减一,您再次运行循环,现在使用之前的函数.

You keep running the loop while you have functions left to do (progress is less than the number of functions), and keep calling each function. Except, if the undo command is used, progress is dropped by one and you run the loop again, with the previous function now.

这篇关于Python 输入提示返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:09