尝试将邮政编码转换为二进制时找不到错误



这是我的代码,我觉得这应该可以工作。我刚才添加了邮政编码 = str(zipcode),看看它是否可以工作,但它没有,所以我可能会把它拿出来,只让原始邮政编码是一个字符串。我需要它是一个字符串,因为我不希望二进制数实际上相互相加。当我在python shell中初始化函数时,它什么也没返回

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

感谢您的任何帮助!

如果我理解正确,邮政编码是一个整数。这是一个将整数转换为二进制的小 python 函数。默认值是需要 24 位二进制。

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)])

例如,我的邮政编码是60517,所以我会这样做:

>>> print int2bin(60517)  
000000001110110001100101 

如果我只想要 16 个二进制位:

>>> print int2bin(60517, 16)
1110110001100101

主要问题是zipcode[n]是一个字符,而不是一个数字,所以它永远不会与数字相等(Python 不会自动转换它们)。您还可以进行许多简化,例如使用 for 循环而不是为字符串编制索引,以及使用字典从十进制数字映射到二进制字符串。

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

我认为您的代码没有任何问题...除了缩进和检查if条件。

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

更新:

您可以将整个事情简化为:

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)

最新更新