我该如何解决:TypeError:object()不带参数?

时间:2016-06-28 23:50:57

标签: python

这是我的计划:

class member:
    name = ""
    phone = ""
    number = 0


    def init(self, name, phone, number):
        self.name = name
        self.phone = phone
        self.number = number


    def setName(self, name):
        self.name = name


    def setPhone(self, phone):
        self.phone = phone


    def setNumber(self, number):
        self.number = number


    def getName(self):
        return self.name


    def getPhone(self):
        return self.phone


    def getNumber(self):
        return self.number


    def displayData(self):
        print("")
        print("Name:", self.name)
        print("Phone:", self.phone)
        print("Jersey Number:", self.number)



def displayMenu():
    print("")
    print("==========Main Menu==========")
    print("1. Display Team Roster.")
    print("2. Add Member.")
    print("3. Remove Member.")
    print("4. Edit Member.")
    print("5. Save Data.")
    print("6. Load Data.")
    print("9. Exit Program.")
    print("")
    return int(input("Selection>  "))

def printMembers(memberList):
    if len(memberList) == 0:
        print("No Current Members in Memory.")
    else:
        for x in memberList.keys():
            memberList[x].displayData()


def addMember(memberList):
    newName = input("Enter new member's name: ")
    newPhone = input("Contact phone number: ")
    newNumber = int(input("Jersey number: "))
    memberList[newName] = member(newName,newPhone,newNumber)
    #An updated list is returned.
    return memberList


def removeMember(memberList):
    removeName = input("Enter member name to be Removed: ")
    #First, check to see if the name given, already exists in the list.
    if removeName in memberList:
        #Use the del key to delete the entry at the given name.
        del memberList[removeName]
    else:
        print("Member not found in list.")
    #An updated list is returned.
    return memberList


def editMember(memberList):
    #get the name of the member to be edited.
    oldName = input("Enter the name of the member yopu want to edit. ")
    #See if name is already in the list, if yes, get the index of the.
    #given name, accept the new one, and replace the name in the list.
    #with the new name.
    #An updated list is returned.
    if oldName in memberList:
        newName = input("Enter the member's new name: ")
        newPhone = input("Contact phone number: ")
        newNumber = int(input("Jersey number: "))
        memberList[oldName] = member(newName, newPhone, newNumber)
    return memberList

def saveData(memberList):
    filename = input("File name to be saved: ")
    print("Saving Data...")
    outFile = open(filename, "wt")
    for x in memberList.keys():
        name = memberList[x].getName()
        phone = memberList[x].getPhone()
        number = str(memberList[x].getNumber())
        outFile.write(name+","+phone+","+number+"\n")
        print("Data saved.")
        outFile.close()

def loadData():
    memberList ={}
    filename = input("Filename to Load: ")
    inFile = open(filename, "rt")
    print("Loading Data...")
    while True:
        #Read in a line of text from the text file.
        inLine = inFile.readline()
        #If the line is empty, stop loading data.
        if not inLine:
            break
        inLine = inLine[:-1]
        name, phone, number = inLine.split(",")
        memberList[name] = member(name, phone, int(number))
    print("Data Loaded Successfully")
    inFile.close()
    return memberList



print("Welcome to the Team Manager")
#Create an empty dictionary by changing tthe [] to {}
memberList = {}
#Get the first menu selection from the user and store it in a control value variable.
menuSelection = displayMenu()


#The main program loop will detect the correct entry from the user and call the appropriate.
#method from the user's selection.
while menuSelection != 9:
    if menuSelection == 1:
        printMembers(memberList)

    elif menuSelection == 2:
        memberList = addMember(memberList)

    elif menuSelection == 3:
        memberList = removeMember(memberList)

    elif menuSelection == 4:
        memberList = editMember(memberList)

    elif menuSelection == 5:
        memberList = saveData(memberList)

    elif menuSelection  == 6:
        memberList = loadData()

    menuSelection = displayMenu()
print ("Exiting Program...")

这是我的错误:

C:\Users\Patrick\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Patrick/PycharmProjects/Week_6_assignment.py/Week_6_assignment.py
Welcome to the Team Manager

==========Main Menu==========
1. Display Team Roster.
2. Add Member.
3. Remove Member.
4. Edit Member.
5. Save Data.
6. Load Data.
9. Exit Program.

Selection>  2
Enter new member's name: margie
Contact phone number: 3134562252
Jersey number: 88
Traceback (most recent call last):
  File "C:/Users/Patrick/PycharmProjects/Week_6_assignment.py/Week_6_assignment.py", line 151, in <module>
    memberList = addMember(memberList)
  File "C:/Users/Patrick/PycharmProjects/Week_6_assignment.py/Week_6_assignment.py", line 74, in addMember
    memberList[newName] = member(newName,newPhone,newNumber)
TypeError: object() takes no parameters

Process finished with exit code 1

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

您将初始化方法命名为init;正确的名称是__init__。双重下划线是Python如何表示为Python“特殊”使用保留的名称。通过不使用正确的名称,调用了超类(object')__init__,但它不带参数,因此您得到错误。

旁注:saveData中有另一个错误;最后printclose调用几乎肯定会缩减,因此您可以编写所有数据,而不只是单个项目。

另外:

while True:
    #Read in a line of text from the text file.
    inLine = inFile.readline()
    #If the line is empty, stop loading data.
    if not inLine:
        break
    inLine = inLine[:-1]
    name, phone, number = inLine.split(",")
    memberList[name] = member(name, phone, int(number))
几乎可以肯定

(在文件顶部导入csv):

for name, phone, number in csv.reader(inFile):
    memberList[name] = member(name, phone, int(number))

为你处理换行符和分割逗号,并且不涉及反复调用.readline()的无限循环的反模式(即使这不是CSV,迭代的正确方法)行是for inLine in inFile:,这避免了whilereadlineif not inLine: break; for inLine in map(str.rstrip, inFile):避免了明确切断尾随换行符的需要。)

答案 1 :(得分:0)

你错过了__。您的

 def init(self, name, phone, number):
      self.name = name
      self.phone = phone
      self.number = number

必须是

def __init__(self, name, phone, number):
      self.name = name
      self.phone = phone
      self.number = number