问题描述
目前我遇到了小于 100 的低范围整数(向下取整到最接近的 10 位)的问题。当我隔离这些函数时,我的 round_down 函数会正确返回预期值。当我将 tup_update_sub 与字典进行比较时,大于 1000 和 100 的整数会正确舍入并更新。当我测试低于 100 的卷时,比较返回 None,而不是正确的整数。
我目前的工作流程是: 接受由先前代码(体积)确定的整数> 有条件地选择除数> 向下舍入该值> 匹配字典中的值> 从字典中返回值> 创建新元组> 替换字典中的元组
我在最后包含了用于调试的打印。我的挫败感是无法调试为什么字典在值低于 100 时无法正确引用。
#dictionary to be updated with new tuple from tup_update_sub
dict_tuberack1={tuberack1['A1']:(14000,104),tuberack1['A2']:(14000,tuberack1['A3']:(14000,104)}
#dictionary to referenced by key for new tuple
vol_tuberack1= {14000: 104,13500: 101,13000: 98,12500: 94,12000: 91,11500: 88,11000: 85,10500: 81,10000: 78,9500: 75,9000: 72,8500: 68,8000: 65,7500: 62,7000: 59,6500: 55,6000: 52,5500: 49,5000: 46,4500: 42,4000: 39,3500: 36,3000: 33,2500: 29,2000: 26,1000: 20,900: 15,800: 15,700: 15,600: 15,500: 15,400: 13,300: 11,200: 9,100: 6,90: 2,80: 2,70: 2,60: 2,50: 1,40: 1,30: 1,20: 1,10: 1}
def round_down(volume,divisor):
vol_even=floor(volume/divisor)*divisor
return vol_even
def tup_update_sub(volume,dict_vol,dict_labware,labware,well):
tup = dict_labware.get(labware[well])
adj_list=list(tup)
adj_list[0]=volume
divisor=1
if volume >=1000:
divisor=1000
vol_even=round_down(volume,divisor)
elif volume <1000 >= 100:
divisor=100
vol_even=round_down(volume,divisor)
else:
divisor=10
vol_even=round_down(volume,divisor)
new_height=dict_vol.get(vol_even)
adj_list[1]=new_height
new_tup=tuple(adj_list)
#modified for debug
print(vol_even) #to show that volume was correctly rounded
print(new_tup) #to show that function has correctly chosen the intended values
print(dict_labware[labware[well]]) #to show that the correct destination was chosen
#so far the script has functioned great when it comes to updating dictionaries with new tuples. The challenge is getting the right value referenced when the rounded value is less than 100.
解决方法
问题出在你代码的这一部分,你调用函数的地方:
if volume >=1000:
divisor=1000
vol_even=round_down(volume,divisor)
elif volume <1000 >= 100:
divisor=100
vol_even=round_down(volume,divisor)
else:
divisor=10
vol_even=round_down(volume,divisor)
看看elif volume <1000 >= 100
。这与 volume = 100 相同。我想你的意思是 elif 100 <= volume < 1000:
您当前的代码永远不会执行 else
部分。因此,对于体积
所以,也简化了
if volume >=1000:
divisor=1000
elif 100 <= volume < 1000:
divisor=100
else:
divisor=10
vol_even = round_down(volume,divisor)