我有两个类似的函数函数def A()
和功能def B()
到目前为止已经写过了,它可以工作,但我想这样做,以便在用户完成在函数 B()
中写入数据后,...他可以选择退出,或者通过在函数 A()
中写入数据来再次开始该过程。
理论上,用户可以在(例如)点击ENTER
之前重复该过程一百万次。退出程序。
我如何实现这一目标?
def A(parameters):
content...
...
...
def B(parameters):
content...
...
...
Press R to repeat with def A (parameters), press Q to quit:
最佳答案
最好将 A()
的功能与 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()
从输入中删除任何前导或尾随空格。
关于python - 完成函数 B 后有条件地调用函数 A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13525771/