【问题标题】:Cannot find error trying to convert zipcode to binary尝试将邮政编码转换为二进制时找不到错误
【发布时间】:2025-12-07 13:25:02
【问题描述】:

这是我的代码,我觉得这应该可以工作。我刚刚添加了 zipcode = 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 中的间距有问题.....
  • 如果您使用str(zipcode),您通过zipcode[n] 获得的元素将是字符,而不是数字。所以像if zipcode[n] == '0' 这样的东西会更好。
  • 这对我一点帮助都没有。最终结果不会是二进制文件,该二进制文件将用于绘制带有海龟图形的条形码
  • 你必须缩进函数的主体。

标签: python string if-statement barcode zipcode


【解决方案1】:

主要问题是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

【讨论】:

    【解决方案2】:

    我没有看到您的代码有任何问题...除了缩进和检查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)
    

    【讨论】:

      【解决方案3】:

      如果我理解正确,邮政编码是一个整数。这是一个将整数转换为二进制的小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
      

      【讨论】:

        最近更新 更多