列表索引超出范围Python函数

拉莫斯15

我一直使列表索引超出范围,而且我的条形码字符串长度检查不起作用。有人告诉我数组内部没有值,但是我不知道如何检查是否有值,如果不存在则如何修复。我是在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!"
TessellatingHeckler

我会尝试回答您的问题-但您尚未提出问题。但是您的代码有几处错误。

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

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在同名的方法中创建一个变量,并为其赋予相同的值。然后,您再分配给条形码,条形码是方法中的局部变量,它恰好与外部“条形码”具有相同的名称。您认为您已经保留了该条形码,因为它具有相同的名称,但是您没有,当该方法完成运行时,结果将丢失。

这是有效的代码,但误导您以为它以一种方式起作用,而实际上却没有。这就是您的代码无法正常工作的原因之一。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python循环:列表索引超出范围

来自分类Dev

(Python)列表索引超出范围-迭代

来自分类Dev

Python:XML列表索引超出范围

来自分类Dev

Python列表索引超出范围-算法

来自分类Dev

列表索引超出范围:Python:

来自分类Dev

Python CSV列表索引超出范围

来自分类Dev

(Python)列表索引超出范围-迭代

来自分类Dev

列表索引超出范围:PYTHON

来自分类Dev

“列表索引超出范围”-Python

来自分类Dev

列表索引超出范围

来自分类Dev

列表索引超出范围

来自分类Dev

列表-索引超出范围

来自分类Dev

列表索引超出范围

来自分类Dev

列表索引超出范围

来自分类Dev

def函数中的空列表(列表索引超出范围)

来自分类Dev

Python字典索引错误:列表索引超出范围

来自分类Dev

Python:列表索引超出范围/追加列表

来自分类Dev

python列表中的IndexError-“列表索引超出范围”

来自分类Dev

Python,列表错误:IndexError:列表索引超出范围

来自分类Dev

索引超出范围-Python

来自分类Dev

为什么函数在 Python 中返回错误“列表索引超出范围”?

来自分类Dev

索引和列表 - 索引超出范围

来自分类Dev

如何解决列表索引超出范围的错误Python?

来自分类Dev

使用模式库的python列表索引超出范围

来自分类Dev

Python IndexError:列表分配索引超出范围

来自分类Dev

Python强制列表索引超出范围异常

来自分类Dev

python中的列表索引超出范围错误

来自分类Dev

python错误:IndexError:列表索引超出范围

来自分类Dev

列表索引超出范围python1