TypeError:'function'类型的对象在python中没有len()

时间:2022-02-28 17:07:58

I am writing a program with python which will do several things such as login as student, login as driver. but when I try to run the code, I face several problems. I am writting down the code below. and also I am giving you the screenshot of the errors. Please help if you can.

我正在用python编写一个程序,它将执行一些操作,例如登录为学生,登录为驱动程序。但是当我尝试运行代码时,我遇到了几个问题。我正在写下面的代码。而且我也给你错误的截图。如果可以的话请帮忙。

Thanks in advance.

提前致谢。

 print "========================================================"
 print "==============Welcome to LiftServer System=============="
 print "========================================================"
 print "\n"
 print "Type start() to View the Options"
 print "\n"



 DriverLogin=[("ali", "ila"), ("bla", "alb")]
 PassLogin=[("ila", "ali")]


 def CorrectDriverLogin(n,x):
  for i in range (len(x)):
    a,b = x[i]
    if (a==n):
        return b

 def CorrectPassLogIn(c,y):
  for j in range (len(y)):
     d,e = y[j]
     if (d==c):
        return e


 def start():
  print "\n"
  print "=====================You are in the System============="
  print "\n"
  print "Choose an option:"
  print "======================================================="
  print "1. Login for Drivers"
  print "2. Login for Students"
  print "3. Exit"
  print "======================================================="
  print "\n"
  choice= raw_input("Enter the choice number:")
  print "\n"

  if (int(choice)==1):
    print""
    DriverLogin()

  elif(int(choice)==2):
    print ""
    PassLogin()

  if (int(choice)==1):
    print" "
    print" "
    p = raw_input (" Current campus: ")
    k = raw_input (" Travelling to: ")
    m = raw_input (" Leaving Time: ")
    x = raw_input (" Enter AM / PM: ")
    a = raw_input (" Number of available seats: ")
    j = raw_input (" Meeting time: ")
    print" \nI am in campus " + p + " ,leaving to campus " + k + " at: " + m + x +" ,where I have only " + a + " seat(s) available."
    print" "
    print"I will be at the reception at: " + j + x + "."
    print" "    
    print"===================================================="
    print"\n"
    print "Send message? "
    print" "
    sendTheMessageD()

elif (int(choice)==2):
    homePage()

 def sendTheMessageD():
    print" "
    print"1. Yes"
    print"2. No"
    choice = raw_input ("Confirm: ")
    print" "
    homePage()

 def PassLogin():
     userPass = raw_input("Username:")
     passName = raw_input("Password:")
     choice=0
     if (userPass==CorrectPassLogIn(passName, PassLogin)):
       print"\n"
       print "=================================================================="
       print "1. Send a Message"
       print "2. View Messages"
       print "3. Log out of the System"
       print "==================================================================="
       print "\n"
       choice = raw_input("Enter the choice's number:")
       print"\n"
     else:
        print"Incorrect Username or Password"
        print"\n"
        print"Try To Log In Again"

     if (int(choice)==1):
        print" "
        sendMessageP()

     elif(int(choice)==2):
        print" "
        viewMessageD()
  def sendMessageP():
        print "======================================================================="
        print "1)Write a message"
        print "2)Go Back"
        print "===========++=========================================================="
        print" "
        choice = raw_input("Enter The choice's Number: ")

      if(int(choice)==1):
        print" "
        y = raw_input("Destination: ")
        c = raw_input("Time: ")
        l = raw_input("Enter AM ? PM: ")
        t = raw_input("Required seats: ")
        print"\n Is there anyone going to campus " + y + " ,at: " + c + l + " ,and has " + t + " seat(s) available."

      elif(int(choice)==2):
        print""
        def sendTheMessageP():
        print" "
        print"1)Yes"
        print"2)No"
        choice = raw_input ("confirm")
        print" "
        homePage()
    def viewMessageD():
        print "======================================================================="
        print"Inbox"
        print" "
        print"1. Message 1"
        print"2. Message 2"
        print "======================================================================="
        choice = raw_input ("Enter the choice's Number:")

      if(int(choice)==1):
        print"\n Message 1 is viewed"
        if(int(choice)==2):
        print"\n Message 2 is viewed"
      else:
        print""

    def DriverLogin():
        xdriver = raw_input("Username:")
        xpass = raw_input("Password:")
        choice =0
      if (xpass==CorrectDriverLogin(xdriver, DriverLogin)):
        print"\n"
        print "============= You are now Logged in as a Driver ================"
        print"Choose an option:-"
        print "======================================================================"
        print "1. Send a Message"
        print "2. View Messages"
        print "3. Log out of the system"
        print "======================================================================"
        print "\n"
        choice = raw_input("Enter the choice's Number:")
      else:
        print"\n"
        print"Incorrect Username or Password"
        print"\n"
        print"You are logged out of the System Try To Log In Again Please"

      if(int(choice)==1):
        print" "
        sendMessageD()

      elif(int(choice)==2):
        print" "

      elif(int(choice)==3):
        print" "

    def homePage():
        print"\n"
        print "======================================================================="
        print "1. Send a Message"
        print "2. View Messages"
        print "3. Log Out of the System"
        print "======================================================================="
        print "\n"
        choice = raw_input("Enter the Choice's Number:")

    def sendMessageD():
        print "======================================================================="
        print"1. Write your message"
        print"2. Go Back"
        print "======================================================================="
        print" "
        choice = raw_input("Enter The Choice's Number:")

And here is the error I am getting: TypeError:'function'类型的对象在python中没有len()

这是我得到的错误:

3 个解决方案

#1


3  

In this line:

在这一行:

if (xpass==CorrectDriverLogin(xdriver, DriverLogin)):

You pass DriverLogin to CorrectDriverLogin without calling it, meaning you're passing the function and not the list/string returned by the function. So when you try and call len on it, it fails.

您将DriverLogin传递给CorrectDriverLogin而不调用它,这意味着您传递的是函数,而不是函数返回的列表/字符串。因此,当您尝试在其上调用len时,它会失败。

This likely happened because you also had a list named DriverLogin earlier in the script:

这可能是因为您在脚本中之前还有一个名为DriverLogin的列表:

DriverLogin=[("ali", "ila"), ("bla", "alb")]

But you've now reassigned that name to be a function:

但是你现在已经将该名称重新分配为一个函数:

def DriverLogin():
    xdriver = raw_input("Username:")
    ...

#2


2  

You named two things DriverLogin:

你将DriverLogin命名为两件事:

DriverLogin=[("ali", "ila"), ("bla", "alb")]
...
def DriverLogin():

When you tried to use the list, you got the function instead:

当您尝试使用该列表时,您获得了该功能:

if (xpass==CorrectDriverLogin(xdriver, DriverLogin)):

Don't reuse names in the same namespace like that. Name your function and your list different things. Same with PassLogin.

不要在同一名称空间中重用名称。为您的功能和列表命名不同的东西。与PassLogin相同。

#3


2  

Answer: You have the function name "DriverLogin" being passed into the "CorrectDriverLogin" function in the main body of the code.

答:您将函数名称“DriverLogin”传递到代码主体中的“CorrectDriverLogin”函数中。

:) This happens to all of us. I'm still learning, and no-one is more mighty than the next person (it's all show).

:)这发生在我们所有人身上。我还在学习,没有人比下一个人更强大(这都是表演)。

#1


3  

In this line:

在这一行:

if (xpass==CorrectDriverLogin(xdriver, DriverLogin)):

You pass DriverLogin to CorrectDriverLogin without calling it, meaning you're passing the function and not the list/string returned by the function. So when you try and call len on it, it fails.

您将DriverLogin传递给CorrectDriverLogin而不调用它,这意味着您传递的是函数,而不是函数返回的列表/字符串。因此,当您尝试在其上调用len时,它会失败。

This likely happened because you also had a list named DriverLogin earlier in the script:

这可能是因为您在脚本中之前还有一个名为DriverLogin的列表:

DriverLogin=[("ali", "ila"), ("bla", "alb")]

But you've now reassigned that name to be a function:

但是你现在已经将该名称重新分配为一个函数:

def DriverLogin():
    xdriver = raw_input("Username:")
    ...

#2


2  

You named two things DriverLogin:

你将DriverLogin命名为两件事:

DriverLogin=[("ali", "ila"), ("bla", "alb")]
...
def DriverLogin():

When you tried to use the list, you got the function instead:

当您尝试使用该列表时,您获得了该功能:

if (xpass==CorrectDriverLogin(xdriver, DriverLogin)):

Don't reuse names in the same namespace like that. Name your function and your list different things. Same with PassLogin.

不要在同一名称空间中重用名称。为您的功能和列表命名不同的东西。与PassLogin相同。

#3


2  

Answer: You have the function name "DriverLogin" being passed into the "CorrectDriverLogin" function in the main body of the code.

答:您将函数名称“DriverLogin”传递到代码主体中的“CorrectDriverLogin”函数中。

:) This happens to all of us. I'm still learning, and no-one is more mighty than the next person (it's all show).

:)这发生在我们所有人身上。我还在学习,没有人比下一个人更强大(这都是表演)。