根据其他值的结果计算值

2021-12-08 04:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个小程序来帮助确定一个新的焊接是否被现有的焊接工艺所覆盖。逻辑是这样的:

焊接工艺试验需要试件。如果该试件通过,这意味着该公司可以焊接一定范围的材料。你知道吗

如果板的thickness为<;=3: 可以从0.7*thickness焊接到2*thickness

如果3<;thickness<;30,则可以从0.5*thickness焊接到2*thickness。你知道吗

如果thickness大于30,则可以焊接大于5的任何东西。你知道吗

resultsdict中的throat_thickness值取决于runs是's'(single)还是'm'(multi)。你知道吗

我的问题是,如果runs=='m'(对于multi),那么throat_thickness的值应该是'no restriction'。你知道吗

# fillet weld
def fw_material_qual(throat_thickness, runs):
    results = {}
    if throat_thickness <= 3 and runs == 's':
        results['mat_thickness_qual'] = (0.7 * throat_thickness, 
                                         1.5 * throat_thickness)
        results['throat_thickness_qual'] = (0.75 * throat_thickness,
                                            1.5 * throat_thickness)
    elif 3 < throat_thickness < 30 and runs == 's':
        results['mat_thickness_qual'] = (0.5 * throat_thickness,
                                         2 * throat_thickness)
        results['throat_thickness_qual'] = (0.75 * throat_thickness,
                                            1.5 * throat_thickness)
    elif throat_thickness >= 30:
        results['mat_thickness_qual'] = '5 and up'
        results['throat_thickness_qual'] = throat_thickness
    return results

所以我的问题是,我能不能用一种更干净的方式来做这件事,而不仅仅是要有更多的elif?你知道吗

    elif throat_thickness <= 3 and runs == 'm': # only m has changed!
        results['mat_thickness_qual'] = (0.7 * throat_thickness, 
                                         1.5 * throat_thickness)
        results['throat_thickness_qual'] = 'no restriction' # only this value changed!

等等。。。你知道吗