当self.Input==0且代码停止向self.binaryList追加“0”时,为什么不应用elif语句?

2024-04-19 00:01:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我对python比较陌生,我正在尝试构建一个二进制代码转换器。代码在self.Input == 0之前工作正常,而elif语句似乎没有被调用。一般来说,我的问题是,在二进制代码的最后1之后,没有将最后几个0追加到self.binaryList

class BinaryCode:
    def __init__(self):
        self.twoSquaredList = [256, 126, 64, 32, 16, 8, 4, 2, 1]
        self.Input = int(input("Number to convert:"))
        self.binaryList = []
        self.x = 9

    def find_number_below_input(self):
        while self.x > 0:
            for number in self.twoSquaredList:
                if number <= self.Input:
                    self.binaryList.append("1")
                    print(self.binaryList)
                    print(number,"number")
                    self.Input -= number
                    print(self.Input,"self.Input")

                elif number > self.Input and len(self.binaryList) > 1:
                    self.binaryList.append("0")

        self.x -= 1

这是输出:

Number to convert:100
['1']
64 number
36 self.Input
['1', '1']
32 number
4 self.Input
['1', '1', '0', '0', '1']
4 number
0 self.Input