根据其他值的结果计算值

时间:2016-02-20 12:17:31

标签: python python-3.x

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

焊接程序测试需要测试件。如果该测试件通过则意味着该公司可以焊接某些范围的材料。

如果牌照的thickness是< = 3:   你可以从0.7 * thickness焊接到2 * thickness

如果3< thickness< 30你可以焊接从0.5 * thickness到2 * thickness

如果thickness是> 30你可以焊接任何大于5的东西。

throat_thickness dict中的results值取决于run是's'(单个)还是'm'(多个)。

我遇到的问题是,如果运行=='m'(对于多个),那么throat_thickness的值应该是'无限制'。

# 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!

等...

1 个答案:

答案 0 :(得分:2)

你可以这样做,

def fw_material_qual(throat_thickness, runs):
    results = {}
    if throat_thickness <= 3:
        results['mat_thickness_qual'] = (0.7 * throat_thickness,
                                         1.5 * throat_thickness)
        if runs == 's':
            results['throat_thickness_qual'] = (
                0.75 * throat_thickness, 1.5 * throat_thickness)
        else:
            results['throat_thickness_qual'] = 'no restriction'