如何在python中重用if检查

如何在python中重用if检查,python,Python,我尝试尽可能少地执行命令或重复使用代码 id = 12345 state, result = commands.getstatusoutput('command -where "(fieldid eq ' + id + ') and (fieldname eq male)"') if state != 0: print "error" newstate, newcom = commands.getstatusoutput('command -where "(fi

我尝试尽可能少地执行命令或重复使用代码

id = 12345
state, result = commands.getstatusoutput('command -where "(fieldid eq ' + id + ') and (fieldname eq male)"')
if state != 0:
        print "error"
        newstate, newcom =  commands.getstatusoutput('command -where "(fieldid eq ' + id + ') and (fieldType like child)"')
        if newstate != 0:
           print "need to create male child"
        else:
            anotherstate, anothercom = commands.getstatusoutput('command -where "(fieldid eq ' + id + ') and (fieldType like child)" -get FatherId')
            master = anothercom.split(' = ')[1]
            masterid = re.findall(r'\d{5}', master)[0]
            # this will return the fathers ID and now i wanna check if the father has a male child, here i would go back up at the first row and enter into the if statements again but now with this new id, but i dont know what to use to do that
            # state, result = commands.getstatusoutput('command -where "(fieldid eq ' + masterid + ') and (fieldname eq male)"')
            # if state != 0:
            #     print "error"
            #     newstate, newcom =  commands.getstatusoutput('command -where "(fieldid eq ' + masterid + ') and (fieldType like child)"')
            #     if fwstate != 0:
            #         print "need to create male child"

    else:
        print "male child exists"
我注释掉了我想替换的块,不知道如何替换,或者用什么替换(参考第一个,我想回到那里,但是用另一个变量masterid)

用于循环?还是定义两个函数

也许这个图表也会有帮助。。。

国家从哪里来?您可以创建一个函数并使用id调用它,因为我想到了参数安全性,但不幸的是,我现在无法更深入地解释我更正了部分代码。。。但更重要的是,我添加了一个链接到一个图表,这是我想要做的。。。我不知道如何准确地编码。。。任何人