1

Here is my code I feel like this should be working. I added the zipcode = str(zipcode) just now to see if it would work which it didnt so I will probably take that out and just have the original zipcode be a string. I need it to be a string because I dont want the binary numbers to actually add to eachother. When I initialize the function in the python shell it just returns nothing

def digitConvert(zipcode):
zipcode = str(zipcode)
n = 0
binary = ""
while n < len(zipcode):
    if zipcode[n] == 0:
        binary = binary + "11000"
        n = n + 1
    elif zipcode[n] == 1:
        binary = binary + "00011"
        n = n + 1
    elif zipcode[n] == 2:
        binary = binary + "00101"
        n = n + 1
    elif zipcode[n] == 3:
        binary = binary + "00110"
        n = n + 1
    elif zipcode[n] == 4:
        binary = binary + "01001"
        n = n + 1
    elif zipcode[n] == 5:
        binary = binary + "01010"
        n = n + 1
    elif zipcode[n] == 6:
        binary = binary + "01100"
        n = n + 1
    elif zipcode[n] == 7:
        binary = binary + "10001"
        n = n + 1
    elif zipcode[n] == 8:
        binary = binary + "10010"
        n = n + 1
    elif zipcode[n] == 9:
        binary = binary + "10100"
        n = n + 1
return binary

Thanks for any help!

joeygmurf
  • 27
  • 1
  • 5

3 Answers3

1

If I understand correctly, the zip code is an integer. Here is a little python function that converts an integer to binary. Default is you want 24 bits of binary.

def int2bin(n, count=24):
    """returns the binary of integer n, using count number of digits"""
    return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])

For example, my zip code is 60517 so I would do this:

>>> print int2bin(60517)  
000000001110110001100101 

If I only want 16 binary bits:

>>> print int2bin(60517, 16)
1110110001100101
Marichyasana
  • 2,966
  • 1
  • 19
  • 20
0

The main problem is that zipcode[n] is a character, not a number, so it will never compare equal to numbers (Python doesn't convert them automatically). There are also a number of simplifications you can make, such as using a for loop instead of indexing the string, and using a dictionary to map from decimal digits to binary strings.

def digitConvert(zipcode):
    zipcode = str(zipcode)
    digitMap = { '0': '11000', '1': '00011', '2': '00101', ... }
    binary = ""
    for digit in zipcode:
        if digit in digitMap:
            binary += digitMap[digit]
    return binary
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I dont see any problem with your code... other than the indents and checking the if conditions.

def digitConvert(zipcode):
    zipcode = str(zipcode)
    n = 0
    binary = ""
    while n < len(zipcode):
        if zipcode[n] == '0':
            binary = binary + "11000"
        elif zipcode[n] == '1':
            binary = binary + "00011"
        elif zipcode[n] == '2':
            binary = binary + "00101"
        elif zipcode[n] == '3':
            binary = binary + "00110"
        elif zipcode[n] == '4':
            binary = binary + "01001"
        elif zipcode[n] == '5':
            binary = binary + "01010"
        elif zipcode[n] == '6':
            binary = binary + "01100"
        elif zipcode[n] == '7':
            binary = binary + "10001"
        elif zipcode[n] == '8':
            binary = binary + "10010"
        elif zipcode[n] == '9':
            binary = binary + "10100"
        n = n + 1
    return binary

Update:

You can simplify the entire thing to this :

def digitConvert(zipcode):
  zipcode = str(zipcode)
  x = { 
    "0" : "11000",
    "1" : "00011",
    "2" : "00101",
    "3" : "00110",
    "4" : "01001",
    "5" : "01010",
    "6" : "01100",
    "7" : "10001",
    "8" : "10010",
    "9" : "10100"
  }
  return "".join(x[i] for i in zipcode if i in x)
dvenkatsagar
  • 936
  • 7
  • 22