0

Basically I need help creating a function to read a given parameter which is a list, go through each digit of the list, checking them and adding their binary value to a different list. I am trying this code, but it's not working the way I think it should. Any help is welcome: The side parameter is there to help sort the binary. I have two sets and depending on which 'side' the digits in the list are on, they have a different binary code.

def bin_convert(upc, side):
    bin_list = []
    if side == 0:    
        for digit in upc:
            if digit == 0:
                bin_list.append(0001101)
            elif digit == 1:
                bin_list.append(0011001)
            elif digit == 2:
                bin_list.append(0010011)
            elif digit == 3:
                bin_list.append(0111101)
            elif digit == 4:
                bin_list.append(0100011)
            elif digit == 5:
                bin_list.append(0110001)
            elif digit == 6:
                bin_list.append(0101111)
            elif digit == 7:
                bin_list.append(0111011)
            elif digit == 8:
                bin_list.append(0110111)
            elif digit == 9:
                bin_list.append(0001011)
        print bin_list
        return bin_list
    else:    
        for digit in upc:
            if digit == 0:
                bin_list.append(1110010)
            elif digit == 1:
                bin_list.append(1100110)
            elif digit == 2:
                bin_list.append(1101100)
            elif digit == 3:
                bin_list.append(1000010)
            elif digit == 4:
                bin_list.append(1011100)
            elif digit == 5:
                bin_list.append(1001110)
            elif digit == 6:
                bin_list.append(1010000)
            elif digit == 7:
                bin_list.append(1000100)
            elif digit == 8:
                bin_list.append(1001000)
            elif digit == 9:
                bin_list.append(1110100)
        print bin_list
        return bin_list
Gabriel
  • 10,524
  • 1
  • 23
  • 28
  • what are you expecting your code to do? – Gabriel Mar 17 '16 at 21:38
  • For example, when the for loop runs through the 'upc' parameter, and a digit, lets say 0 comes up, I need it to append the correct binary related to that digit to the bin_list. So if the side parameter is 0 and the number the loop is reading is a '1', then the value 0011001 should be appended to the bin_list, but it doesn't work.. – Jordan Bentley Mar 17 '16 at 21:41
  • integers with leading zeros are interpreted as octal ... see http://stackoverflow.com/questions/11620151/what-do-numbers-starting-with-0-mean-in-python – Gabriel Mar 17 '16 at 21:47
  • if you want to preserve your sequence of 1's and 0's you should probably use a string `bin_list.append('0011001')`. You could also use the integer that the binary represents, `bin_list.append(int('0011001', 2))` – Gabriel Mar 17 '16 at 21:48
  • Okay, I'll try that. When the list prints there at the end, right before the value is returned both still are printing empty, I'm not sure whats happening – Jordan Bentley Mar 17 '16 at 21:51
  • if your `bin_list` is empty my guess is that `upc` is empty or none of the elements of `upc` are equal to the digits from 0-9. it would probably be a good debugging step to print the values of `upc` and `side` in your function. – Gabriel Mar 17 '16 at 21:55
  • Both returned the expected values when they printed, but it's acting like none of the elements equal 0-9 but they do. The values that got returned were, in this case, 886971. The for loop should run through each of those digits individually right? – Jordan Bentley Mar 17 '16 at 21:59
  • it would help if you edited your post to include exactly what `upc` and `side` are equal to and any error messages you're getting – Gabriel Mar 17 '16 at 22:06
  • I got it, my parameter was coming in as a string, but my for loop check was expecting integers. Thank you for all your help! – Jordan Bentley Mar 17 '16 at 22:09
  • you're welcome. i'm flagging this question for removal as its unlikely to be helpful to anyone else. – Gabriel Mar 17 '16 at 22:13

0 Answers0