這是我的計劃:如何解決: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
有什麼建議?
你不給我們介紹了代碼,也不要試圖調試或顯示您的調試嘗試。最重要的是,我們不會在沒有努力的情況下進行分羹或修改任務 – Li357
關於python的一件很酷的事情是你並不需要編寫setter和getter,你只需要編寫這些代碼就可以了想要控制從你的班級設置或獲得什麼 – Copperfield