python - 完成函数 B 后有条件地调用函数 A

标签 python repeat function

我有两个类似的函数函数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() 将用户输入的所有内容都返回为小写字符,从而允许将 rR 视为相同的。

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

关于python - 完成函数 B 后有条件地调用函数 A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13525771/

相关文章:

python - 将字符串转换为元组

python - Python 中的 Levenshtein 距离循环

function - 确保kotlin方法是静态的,顶级的或带注释的@JvmStatic

jquery - 如何在 jQuery 中声明和使用函数

python - Numpy matmul - 当前不支持对象数组

python - 自定义 Django 命令的自定义位置

python - 使用 Highcharts 的 Django Chart

java - 在 java (android) 中不重复生成随机数组 String[]

重复 data.frame 的行

scala - 如何使 Scala 控制抽象重复直到?