2013-09-24 31 views
-5

我的程序出了问题,真的很烦我我一直在收到一个错误,我甚至不知道为什么你可以帮我吗?int没有len错误请帮助我吗?

我不断收到builtins.TypeError:类型“诠释”的对象没有LEN()错误

当它到达密码verfication阶段,我无法弄清楚的问题是什么..

#Program asking for a username, password and security pin 
#Written by Cole Johnston 
#19/08/13 - 

#Declare/Initialize variables 
sFirstName = "" #User's input of their first name (the minimum is 2 characters) (string) 
sLastName = "" #User's input of their last name (the minimum is 4 characters) (string) 
sPassword = "" #User's input of their password (it must have a minimum length of 6 characters and a maximum length of 10 characters. It must contain at least 2 numbers in the password, it is case sensitive) 
iPinNo = 0 #User's chosen input of security pin (It must be 4 characters long and contain only numbers which are single digit and range between 0-9) 
iCount = 0 #The amount of times the user enters an invalid username (integer) 
sMessage = "" #Message for user (string) 
iFirstNameCheck=0 #Checks first name 
iLastNameCheck=0 #Checks last name 
iPasswordCheck=0 #Checks password 
iPinNoCheck=0 #Checks pin 
sUserName = "" #Creates a username for the user (uses first and last name) 
sPasswordVerification = "" #Checks the password is correct and same as the first one 

def APFirstERROR(): 
    print(("ERROR: Incorrect Attemps consisting of 3 \n\t Closing program")) 
    sys.ext 


def APSecondERROR(): 
    print (("ERROR: Incorrect Attemps consisting of 3 \n\t Closing program")) 
    sys.ext 

def APThirdERROR(): 
    print (("ERROR: Incorrect Attemps consisting of 3 \n\t Closing program")) 
    sys.ext 

def APFourthERROR(): 
    print (("ERROR: Incorrect Attemps consisting of 3 \n\t Closing program")) 
    sys.ext 


#Ask for the users First name 

while len(sFirstName) <2: 
    sFirstName=input("Could you please enter your first name: ") 
    if len(sFirstName) <2: 
     print("\tERROR: I'm sorry, you must enter a minumum of two characters") 
     FirstName = "" 
     iFirstNameCheck = iFirstNameCheck +1 
     print (iFirstNameCheck, "Incorrect attempt") 
     if iFirstNameCheck == 3: 
      APFirstERROR() 

#Ask for the users Last name   
while len(sLastName) <4: 
    sLastName = input("Could you please enter your last name: ") 
    if len(sLastName) <4 : 
     print ("\tERROR: I'm sorry, you must enter a minumum of four characters") 
     LastName = "" 
     iLastNameCheck = iLastNameCheck +1 
     print (iLastNameCheck, "Incorrect attempt") 
     if iLastNameCheck==3: 
      APSecondERROR()  

sUsername = "Congratulations, your username will be " + sLastName[0:4] + sFirstName[0:1] 
print (sUsername)    


while sPassword is "": 
    sPassword=input("Could you please enter a password: ") 
    if len(sPassword) <6 or len(sPassword) >10 : 
     print("ERROR: I'm sorry, your password must be 6 characters or more but not exceeding 10 characters.\nPlease try again") 
     if sum(c.isdigit() for c in sPassword) <2: 
      print("Im sorry, your password does not contain enough numbers.\nThe requirements are a minimum of 2 numbers")   
      sPassword="" 
      iPasswordCheck=iPasswordCheck+1 
      print(iPasswordCheck, "Incorrect Attempt") 
      if iPasswordCheck ==3: 
       sPassword="Incorrect" 
       APErrorThird()    
       break 

#Verify the password 
while sPasswordVerification is "": 
    sPasswordVerification=input("Please enter your password a second time for verification: ") 
    if sPasswordVerification==sPassword: 
     print("That password is Accepted") 
     if not sPasswordVerification==sPassword: 
      sPasswordVerification="" 
      print ("I'm sorry, that password is: Not accepted") 

       a 
#ASk user for pin creation 
while len(iPinNo) <4: 
    iPinNo= input("Now could you please create a pin number. The requirements are that it must be only numbers (0-9). Must be 4 numbers long\n\tpin=?\t\n:") 
    if not re.match("^[1-9]*$", iPinNo): 
     print ("ERROR: I'm sorry but only numbers between 1-9 are allowed!") 
     if len (iPinNo) <4: 
      print ("I'm sorry, your pin number MUST be 4 characters.") 
      iPinNoCheck=iPinNoCheck+1 
      print (iPinNoCheck, "Incorrect Attempt") 
      if iPinNoCheck==3: 
       iPinNo ="Incorrect" 
       APErrorFourth()     

#Display the Username, the Password and the Pin number 

print ("Okay, " + (sFirstName) +" Your username is: ",sUsername) 
print("And, Your password is: ",sPassword) 
print ("Finally , Your Security pin is: ",iPinNo)  
+0

你应该考虑“初始化”的变量'None',然后用'测试,如果x不是无:'而不是0和空字符串初始化 – jedwards

+1

之间的混合请将您的问题进一步本地化。当然,并不是所有的代码都与问题相关,而且您甚至没有指出错误发生在哪条线上,或者您尝试调试某些东西。 (例如,声明中变量的值失败了吗?不要要求我们在头脑中模拟您的代码。) – millimoose

+0

另外:在'while'下嵌套'if'的三个级别相当糟糕。更重要的是,您可以清楚地知道函数,因为您正在将它们用于错误报告。 – millimoose

回答

4

这里:while len(iPinNo) < 4:

iPinNo是一个整数,你不应该来调用整数len()(因此错误):传递给len()的对象必须是一个序列或映射。

首先将它转换为字符串,使用str(),然后调用len()来获取数字量。

+2

虽然这是真的,但一个突出的问题是*为什么*是'iPinNo'是一个整数?答案是因为OP使用'input'并应该使用'raw_input'。 – kojiro

+1

@kojiro OP正在使用Python 3.'iPinNo'是一个整数,因为他在开始时'iPinNo = 0' – TerryA

+0

你是否说因为他在'print'“参数”周围使用括号?这可能是Python 2.7。 – kojiro

0

那么,粘贴实际的回溯是非常有用的,这样我们就可以看到堆栈中的哪个位置(以及代码的哪一行)引发异常。

无论如何,在你的代码中,你需要多次询问整数的长度。例如

while len(iPinNo) < 4: 

int类型没有长度。如果你想确保数为4个位数,你可以做

while len(str(iPinNo)) < 4: 

while 999 < iPinNo < 10000: 
0

要得到的十进制数的位数,使用(基数为10)对数。

from math import log10, ceil 
ceil(log10(iPinNo)) 

0的对数是未定义的,所以你必须在测试长度之前检查一下。


无论如何,为什么不仅仅是针对10的匹配能力进行测试呢?

while iPinNo < 10000: 

while iPinNo < 10**5 
+0

heh,也许一个PyInt应该真的有一个'__len __(self,radix = 10)'方法。 :) – kojiro

+0

@kojiro:已经有了'bit_length';实现'digit_length(radix = 10)'将非常简单。但我不会把它称为'__len__' ... – abarnert

+0

@abarnert没有人理解我的幽默感,特别是当涉及到不适当的指挥Python的可扩展性时。 – kojiro