2016-06-28 69 views
0

這是我的計劃:如何解決:TypeError:object()不帶參數?

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 

有什麼建議?

+0

你不給我們介紹了代碼,也不要試圖調試或顯示您的調試嘗試。最重要的是,我們不會在沒有努力的情況下進行分羹或修改任務 – Li357

+0

關於python的一件很酷的事情是你並不需要編寫setter和getter,你只需要編寫這些代碼就可以了想要控制從你的班級設置或獲得什麼 – Copperfield

回答

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:,它避免了while,readlineif not inLine: break; for inLine in map(str.rstrip, inFile):也避免了顯式切斷尾部換行符的需要)。

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 
+0

誰說神不居於我們之中僅僅是凡人!!!!! –

+0

我這樣做了,我的新錯誤:C:\ Users \ Patrick \ AppData \ Local \ Programs \ Python \ Python36-32 \ python.exe C:/Users/Patrick/PycharmProjects/Week_6_assignment.py/Week_6_assignment.py 歡迎來到團隊經理 ==========主菜單========== 1.展示團隊名單。 2.添加成員。 3.刪除成員。 4.編輯會員。 5.保存數據。 6.加載數據。 9.退出程序。 選擇> 2 回溯(最近最後調用): 文件 「C:/Users/Patrick/PycharmProjects/Week_6_assignment.py/Week_6_assignment.py」,線151,在 會員=使用addMember(會員) NameError:名字'addMember'不是dfnd –

+0

我的不好。回溯(最近一次通話最後): 文件「C:/Users/Patrick/PycharmProjects/Week_6_assignment.py/Week_6_assignment。PY」,線151,在 會員=使用addMember(會員) NameError:名稱‘使用addMember’沒有定義 過程,退出代碼1 –

相關問題