当我的函数尝试从舍入值函数中引用带有整数的字典时,我的函数不一致

My function is inconsistent when it tries to reference a dictionary with an integer from a rounded value function

我的目标是构建一个函数,最终用新元组替换字典中的元组。

目前,我遇到小于 100 的小范围整数的问题,该整数向下舍入到最接近的第 10 位。当我隔离功能时,我的 round_down 功能正确 returns 预期值。当我将 tup_update_sub 与字典进行比较时,大于 1000 和 100 的整数会正确向下舍入并更新。当我测试低于 100 的交易量时,比较 returns None,而不是正确的整数。

我目前的工作流程是: 接受一个由先前代码确定的整数(体积)>有条件地选择一个除数>将值向下舍入>匹配字典中的值> returns字典中的值>创建新元组>替换字典中的元组

我在最后包含了用于调试的打印。我的挫败感是无法调试为什么当值低于 100 时字典无法正确引用。


#dictionary to be updated with new tuple from tup_update_sub
dict_tuberack1={tuberack1['A1']:(14000,104), tuberack1['A2']:(14000,104), 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。这与体积 < 1000 和 1000 >= 100 相同。我想你的意思是 elif 100 <= volume < 1000: 您当前的代码从不执行 else 部分。所以对于体积 < 100 的除数是 100,而不是你想的 10。

所以,也简化了

  if volume >=1000:
      divisor=1000
  elif 100 <= volume < 1000:
      divisor=100
  else:
      divisor=10
  vol_even = round_down(volume,divisor)