1

到目前为止,我有两个类似的函数 functiondef A()和 function def B(),它可以工作,但我想这样做,以便用户在 function 中完成写入数据后B(),......他可以选择退出或开始在函数中写入数据再次处理A()

因此,理论上,用户可以在他(例如)点击ENTER退出程序之前重复该过程一百万次。

我将如何实现这一目标?

def A(parameters):
    content...
    ...
    ...

def B(parameters):
    content...
    ...
    ...

Press R to repeat with def A (parameters), press Q to quit:
4

2 回答 2

1

合并A()with的功能B()并传递一个标志可能会更好,但是这里有一个解决方案,允许在用户点击之前A()调用:B()RETURN

def A():
    print 'Processing in A!'

def B():

    choice = ''
    print 'Processing in B!'

    while choice.lower().strip() != 'r':    
        choice = raw_input("Press R to repeat, RETURN to exit: ").lower().strip()            
        if choice == '':
            return False
        if choice  == 'r':
            return True

while B():
    A()

输出:

Processing in B!
Press R to repeat, RETURN to exit: R
Processing in A!
Processing in B!
Press R to repeat, RETURN to exit: r
Processing in A!
Processing in B!
Press R to repeat, RETURN to exit: notR
Press R to repeat, RETURN to exit: 

一些注意事项:

lower()返回用户键入的任何内容,因为所有小写字符都允许r并被R视为相同。

strip()从输入中删除任何前导或尾随空格。

于 2012-11-23T09:17:48.593 回答
1

怎么样:

i = "r"
while i != "q":
    A()
    B()
    i = raw_input("Press Q to quit, press any other key to repeat with def A (parameters):").lower().strip()
于 2012-11-23T09:28:26.367 回答