【问题标题】:TypeError: Object of type 'function' has no len() in pythonTypeError:“函数”类型的对象在 python 中没有 len()
【发布时间】:2014-07-01 19:54:02
【问题描述】:

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

提前致谢。

 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:")

这是我得到的错误:

【问题讨论】:

  • 还没看代码,记得调用函数吗?
  • 你的缩进被破坏了。无法判断每个块的开始或结束位置或位置嵌套了什么。
  • 问题似乎是你有两个列表和两个同名的函数。
  • 停止投票,人们。这是一个合法的主题问题,并且由于 OP 声明了两个名为 DriverLogin 的对象,因此堆栈跟踪并不立即清楚。

标签: python function typeerror


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你命名了两个东西DriverLogin:

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

    当你尝试使用列表时,你得到了函数:

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

    不要像这样在同一个命名空间中重复使用名称。命名你的函数和你的列表不同的东西。与PassLogin 相同。

    【讨论】:

      【解决方案3】:

      在这一行:

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

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

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

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

      但您现在已将该名称重新分配为函数:

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

      【讨论】:

      • 他对PassLogin也有同样的问题。
      猜你喜欢
      • 2015-01-21
      • 2021-03-03
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多