我正在编写一个小程序来帮助确定现有焊接工艺是否覆盖了新焊缝。逻辑是这样的:
焊接程序测试需要测试件。如果该测试件通过则意味着该公司可以焊接某些范围的材料。
如果牌照的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!
等...
答案 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'