我总是让列表索引超出范围,而且我的条形码字符串长度检查也不起作用。我被告知数组中没有值,但我不知道如何检查是否有值,如果没有,如何修复它。我对在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!"
我想试着回答你的问题-但你一个问题都没问。但是你的代码有几个地方出错了。你知道吗
列表索引超出范围来自此位:
您的“条形码长度检查”是:
你没有解释“它不工作”是什么意思,但是你写它是为了只检查“长度大于5”的条形码。它没有说明禁止长度为0、1、2、3、4的条形码,所以它会将这些条形码视为有效的,也没有说明确保它们是二进制数(例如,只包含0和1)。你知道吗
的确如此。您离强制使用5个字符的数字并使此代码基本上“起作用”已经不远了。但是你离读者理解的快乐代码还有很长的路要走。你知道吗
我不知道该放什么在这里,你需要一个很好的教程和一些推动使用交互式口译员(Python.exe、/usr/bin/Python、IDLE、PythonWin等等)来以“实时”方式玩代码,而不必先编写大量脚本。你知道吗
举个例子:
您将
number
传递到__init__
,然后对它不做任何操作。你找到了在类之前定义的zipcode
,它在外部,在不同的范围中。你可以阅读它,它是有效的,但是它的设计很糟糕-类和对象可以用整洁的包装来包装代码。让比特从包装中伸出并获取随机值是一个问题。你知道吗下一种方法就是这样的问题:
这样做的目的是到达类定义上方的全局zipcode,然后在同名的方法中创建一个
zipcode
变量,并赋予它相同的值。然后,您分配给barcode,这是方法中的一个局部变量,它恰好与外部的“barcode”同名。您认为您保留了这个条形码是因为它具有相同的名称,但是您没有,当方法完成运行时,结果将丢失。你知道吗它是有效的代码,但它会误导你认为它是单向的,而不是单向的。这也是你的代码不起作用的一个原因。你知道吗
在类方法中的任何地方使用
self.whatever
都是一个很好的计划。EncodeNumber应该使用您放在__init__
中的self.number
。你知道吗它们应该返回
self.zipcode
或类似的值。你知道吗在这里,ZipCode(x)返回一个对象,而不是保存在变量中。对returnBarCode()的调用没有调用任何内容。它需要更像:
相关问题 更多 >
编程相关推荐