列表索引超出范围Python函数

时间:2014-11-29 03:02:43

标签: python

我一直让List Index超出范围,而且条形码的字符串长度检查也不起作用。我被告知阵列中没有值,但我不知道如何检查是否有值以及如果没有值,如何修复它。我是python中使用类的新手。

 class ZipCode():
        def __init__(self,number):
                if (encode == "Yes" or encode == "yes"):
                        self.number = 1
                else:
                        self.number = ""
        def EncodeNumber(self):
                self.number = str(self.number)
                a = []
                a += list(self.number)
                zipcode1 = int(a[0])
                zipcode2 = int(a[1])
                zipcode3 = int(a[2])
                zipcode4 = int(a[3])
                zipcode5 = int(a[4])
                barcode1 += `(zipcode1 / 7)`
                barcode1 += `(zipcode1 / 4)`
                barcode1 += `(zipcode1 / 2)`
                barcode1 += `(zipcode1 / 1)`
                if (barcode1.count("1") >= 2):
                        barcode1 += "0"
                else:
                        barcode1 += "1"
                barcode2 += `(zipcode2 / 7)`
                barcode2 += `(zipcode2 / 4)`
                barcode2 += `(zipcode2 / 2)`
                barcode2 += `(zipcode2 / 1)`
                if (barcode2.count("1") >= 2):
                        barcode2 += "0"
                else:
                        barcode2 += "1"
                barcode3 += `(zipcode3 / 7)`
                barcode3 += `(zipcode3 / 4)`
                barcode3 += `(zipcode3 / 2)`
                barcode3 += `(zipcode3 / 1)`
                if (barcode3.count("1") >= 2):
                        barcode3 += "0"
                else:
                        barcode3 += "1"
                barcode4 += `(zipcode4 / 7)`
                barcode4 += `(zipcode4 / 4)`
                barcode4 += `(zipcode4 / 2)`
                barcode4 += `(zipcode4 / 1)`
                if (barcode4.count("1") >= 2):
                        barcode4 += "0"
                else:
                        barcode4 += "1"
                barcode5 += `(zipcode5 / 7)`
                barcode5 += `(zipcode5 / 4)`
                barcode5 += `(zipcode5 / 2)`
                barcode5 += `(zipcode5 / 1)`
                if (barcode1.count("1") >= 2):
                        barcode5 += "0"
                else:
                        barcode5 += "1"
                self.number = "1" + barcode1 + barcode2 + barcode3 + barcode4 + barcode5 + "1"
        def DecodeNumber(self):
                b = []
                b += list(self.number)
                barcode1 = int(b[0])
                barcode2 = int(b[1])
                barcode3 = int(b[2])
                barcode4 = int(b[3])
                barcode5 = int(b[4])
                self.number = ((barcode1 * 7) + (barcode2 * 4) + (barcode3 * 2) + (barcode4 * 1) + (barcode5 * 0 ))

        def returnZipCode(self):
                return self.number
        def returnBarCode(self):
                return self.number
if __name__ == "__main__":
        encode = "Yes"
        encode = raw_input("If you would like to encode a zip code type Yes, else type No:")
        if (encode == "yes" or encode == "Yes"):
                x = raw_input("Enter the Zipcode you would like to be encoded!:")
                ZipC = ZipCode(x)
                ZipC.EncodeNumber()
                ZipC.returnBarCode()
                print "Holy Jeebus It worked!"
        else:
                x = raw_input("Enter a 5 digit binary number you would like to be decoded based on POSTNET standards!:")
                while (len(x) != 5):
                        print "Invalid Barcode!"
                        x = raw_input("Enter a 5 digit binary number you would like to be decoded based on POSTNET standards!:")         
                ZipC = ZipCode(x)
                ZipC.DecodeNumber()
                ZipC.returnZipCode()
                print "Holy Jeebus It worked!"

1 个答案:

答案 0 :(得分:1)

我会尝试回答你的问题 - 但你没有问过一个问题。但是你的代码有几个错误。

列表索引超出范围来自此位:

zipcode = 1                  
ZipCode(x).EncodeNumber()    
zipcode = str(zipcode)
a = []                       # inside EncodeNumber
a += zipcode                 # a is now a list with one item, a string 
                             # representation of a number in it e.g. ["34567"]
zipcode1 = int(a[0])         # a[0] is the string "4567"
zipcode2 = int(a[1])         # a[1] is an error

您的“条形码长度检查”是:

while (len(x) > 5):
    print "Invalid Barcode!"
    x = raw_input("Enter a 5 digit binary number you would like to be decoded based on POSTNET standards!:")         

你没有解释“它不起作用”的意思,但是你已经写了它来检查只有“大于长度5”的条形码它没有说禁止长度为0,1,2,3的条形码,4,所以它会将它们视为有效,并且它不会确保它们是二进制数(例如仅包含0和1)。

  

我是python中使用类的新手。

事实上。你不是太远了强迫一个5个字符的数字,并使这个代码基本上“工作”。但是,距离对读者有意义的快乐代码还有很长的路要走。

我不知道该把什么放在这里;你需要一个好的教程和一些推动使用交互式解释器(Python.exe,/ usr / bin / Python,IDLE,PythonWin或其他)来“实时”使用代码,而无需先编写大量脚本。

仅举例:

barcode = ""
zipcode = 1
encode = "Yes"
encode = raw_input("If you would like to encode a zip code type Yes, else type No:")

class ZipCode():
        def __init__(self,number):
                if (encode == "Yes" or encode == "yes"):
                        self.number = 1
                        self.number = zipcode
                else:
                        self.number = ""
                        self.number = barcode

您已将number传递给__init__,然后对此无效。你到达的是zipcode,它是在课前定义的,它在外面,在不同的范围内。你可以读取它,它可以工作,但它的设计很糟糕 - 类和对象可以用整齐的包装来包装代码。让比特通过包装伸出并抓住随机值就会产生问题。

接下来的方法就是问题:

        def EncodeNumber(self):
                zipcode = str(zipcode)
                ..
                barcode = "1" + barcode1 + barcode2 + barcode3 + barcode4 + barcode5 + "1"

这样做的目的是在类定义上方找到全局压缩代码,然后在同名方法中创建一个zipcode变量,并赋予它相同的值。然后,您将为条形码分配,该条形码是方法中的 local 变量,它恰好与外部“条形码”具有相同的名称。你认为你保留了这个条形码,因为它具有相同的名称,但你没有,当方法完成运行时,结果会丢失。

这是有效的代码,但它误导你认为它以一种方式工作,而不是。这就是你的代码不起作用的一个原因。

在类方法中的任何地方使用self.whatever都是一个很好的计划。 EncodeNumber应该使用您放在self.number内的__init__

        def returnZipCode(self):
                return zipcode
        def returnBarCode(self):
                return barcode

这些应该返回self.zipcode或类似。

if __name__ == "__main__":
        if (encode == "yes" or encode == "Yes"):
                x = raw_input("Enter the Zipcode you would like to be encoded!:")
                ZipCode(x).EncodeNumber()
                returnBarCode()

这里,ZipCode(x)返回一个对象,您不将其保存在变量中。你对returnBarCode()的调用没有调用任何东西。它需要更像:

zipC = ZipCode(x)
zipC.EncodeNumber()
zipC.returnBarCode()