Search code examples
pythondictionarytuplesroundingrounding-error

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


My goal is to build a function that ultimately replaces a tuple in a dictionary with a new tuple.

Currently I am having issues with low range integers of less than 100 that round down to the nearest 10th. When I isolate the functions, my round_down function correctly returns the intended value. When I have tup_update_sub compare to a dictionary, integers above 1000 and 100 correctly round down and update. When I test volumes below 100, the comparison returns None, instead of the correct integer.

My current workflow is: takes in an integer determined from previous code (volume)> conditionally chooses a divisor> rounds down the value> matches the value in a dictionary> returns the value from the dictionary> new tuple created> tuple in dictionary is replaced

I included print for debug at the end. My frustration is not being able to debug why the dictionary won't reference correctly when the value is below 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. 



Solution

  • The problem is in this part of your code, where you call the function:

       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) 
    

    Look at elif volume <1000 >= 100. This is same as volume < 1000 and 1000 >= 100. I think you mean elif 100 <= volume < 1000: Your current code never execute the else part. So for volume < 100 the divisor is 100, not 10 as you think.

    So, also simplified

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