在追踪转换错误方面需要帮助

时间:2014-01-19 12:26:38

标签: python binary project

我知道这可能有点令人困惑,但我是Python的新手。我有一个学校项目的任务是创建一个二进制到denary的转换器,它可以获取最多255个二进制和最多8位二进制数。

我基本上接受输入,拆分,检查,将其放入列表,解决,将其放入另一个列表并打印完成列表。这一切都与最后一部分 - 转换有关。提前谢谢。

我在两次转化时都收到此错误:

    Traceback (most recent call last):
  File "C:\Users\massi_000\Desktop\COM\Task 1 #2.5.py", line 101, in <module>
    main()
  File "C:\Users\massi_000\Desktop\COM\Task 1 #2.5.py", line 91, in main
    checkUp(noToConvert, mainOp)
  File "C:\Users\massi_000\Desktop\COM\Task 1 #2.5.py", line 80, in checkUp
    binaryToDenary(noToConvert)
  File "C:\Users\massi_000\Desktop\COM\Task 1 #2.5.py", line 30, in binaryToDenary
    while int(doneListBin[int(r)]) > 1:
  IndexError: list index out of range

这是我的完整代码。

doneListBin = []
doneListDen = []
splitList = []
doneList = []


def denaryToBinary(splitList):
    #Works binary
    for r in splitList:
        doneListDen.append(int(splitList[int(r)]) % 2)
        while int(doneListDen[int(r)]) > 1:
            doneListDen.append(int(splitList[int(r)]) % 2)
    print(doneListDen)

def binaryToDenary(splitList):
    #Works denary
    for r in splitList:
        doneListBin.append(int(splitList[int(r)]) * 2 ** (int(r) + 1))
        while int(doneListBin[int(r)]) > 1:
            doneListBin.append(int(doneListBin[int(r)]) * 2 ** (int(r) + 1))
    print(doneListBin)

所有精美的过去

def checkUp(noToConvert, mainOp):
    try:
        if int(noToConvert) < 0:
            raise ValueError

        elif noToConvert.isdigit() == False:
            raise ValueError

        elif mainOp == 'd':
            if int(noToConvert) > 255:
                raise ValueError

            else:
                #Splits input for checking & passes
                splitList.extend(noToConvert)
                print(splitList)
                denaryToBinary(noToConvert)


        elif mainOp == 'b':


            #Makes sure it's not huge
            if int(len(str(noToConvert))) > 8:
                raise ValueError

            #Splits input for checking
            splitList.extend(noToConvert)
            print(splitList)

            #Makes it 8-bit
            if int(len(noToConvert)) < 8:
                for i in range(8 - int(len(noToConvert))):
                    splitList.insert(0, 0)
            #while int(len(str(splitList))) < 8:
             #   splitList.insert(0, 0)
               # print(splitList)


            #Should only accept valid inputs (0s and 1s)
            for i in splitList:
                if int(splitList[int(i)]) == 0 or int(splitList[int(i)]) == 1:
                    print(str(i) + ' in range')

                else:
                    raise ValueError
            binaryToDenary(noToConvert)
    except ValueError:
        main()


def main():
    ###     CHOICES     ###
    mainOp = input("Do you want to convert from (D)enary or from (B)inary?: ").lower()
    noToConvert = input("What is the number to convert?: ")

    if mainOp in ("d", "denary", "b", "binary"):
        checkUp(noToConvert, mainOp)


    else:
        raise ValueError



        #   EXECUTION    #
main()

1 个答案:

答案 0 :(得分:1)

你可以尝试

def binaryToDenary(splitList):
    doneListBin = []
    for r in range(0, len(splitList)):
        doneDigit = int(splitList[int(r)]) * 2 ** (len(splitList)-int(r)-1)
        doneListBin.append(doneDigit)
    print sum(doneListBin)

同样在checkUp(noToConvert,mainOp)中,你应该使用binaryToDenary(splitList)而不是binaryToDenary(noToConvert)

对于二元否定

def denaryToBinary(noStr):
    #Works binary
    doneListDen = []
    noToConvert = int(noStr)
    while(noToConvert>0):
       doneListDen.append(noToConvert%2)
       noToConvert = noToConvert // 2
    doneListDen.reverse()
    print doneListDen