这是在课程" Roster"
中定义的功能class Roster:
def __init__(self, name, phone, jersy):
self.name = name
self.phone = phone
self.jersy = jersy
def setName(self, name):
self.name = name
def setPhone(self, phone):
self.phone = phone
def setnumber(self, jersy):
self.number = jersy
def getName(self):
return self.name
def getPhone(self):
return self.phone
def getNumber(self):
return self.jersy
def displayMenu(self):
print ("==========Selection Menu==========")
print ("1. Display the Roster")
print ("2. Add a Player to the Roster")
print ("3. Remove a Player from the Roster")
print ("4. Change a Player Name displayed in the Roster")
print ("5. Load the Roster")
print ("6. Save the Roster")
print ("7. Quit")
print ()
return int (input ("Selection>>> "))
def displayRoster(self):
print ("****Team Roster****")
print ("Player's Name:", self.name)
print ("Player's Telephone number:", self.phone)
print ("Player's Jersey number:", self.jersy)
这是代码:(我知道你不必为了导入"将一个类导入自身,所以没有导入调用)
Players = {}
def addPlayer(Players):
newName = input ("Add a player's Name: ")
newPhone = input ("Phone number: ")
newNumber = input ("Jersey number: ")
Players[newName] = newName, newPhone, newNumber
return Players
def removePlayer(Players):
removeName = input ("What name would you like to remove? ")
if removeName in Players:
del Players[removeName]
else:
print ("Name was not found!")
return Players
def editPlayer(Players):
oldName = input ("What name would you like to change? ")
if oldName in Players:
newName = input ("What is the new name? ")
newPhone = input ("Phone number: ")
newNumber = input ("Jersey number: ")
Players[newName] = newName, newPhone, newNumber
del Players[oldName]
print ("***", oldName, "has been changed to", newName)
else:
print ("Name was not found!")
return Players
def saveRoster(Players):
print("Saving data...")
outFile = open("D:\Documents\Grantham\Python Projects\Python Week Six\Roster.txt", "wt")
for x in Players.keys():
name = Roster.getName(Players)
phone = Roster.getPhone(Players)
jersy = Roster.getNumber(Players)
outFile.write("name+","+phone+","+jersy+","\n")
print("Data saved.")
outFile.close()
return Players
def loadRoster():
Players = {}
filename = input("Filename to load: ")
inFile = open(Filename(Players), "rt")
print("Loading data...")
while True:
inLine = inFile.readline()
if not inLine:
break
inLine = inLine[:-1]
name, phone, jersy = inLine.split(",")
Players[name] = RosterClass.Players(name, phone, jersy)
print("Data Loaded Successfully.")
inFile.close()
return Players
print ("Welcome to the Team Manager")
menuSelection = Roster.displayMenu ('Players')
while menuSelection != 7:
if menuSelection == 1:
myRoster.displayRoster (Players)
elif menuSelection == 2:
Players = addPlayer (Players)
elif menuSelection == 3:
Players = removePlayer (Players)
elif menuSelection == 4:
Players = editPlayer (Players)
elif menuSelection==5:
loadRoster(Players)
elif menuSelection==6:
saveRoster(Players)
menuSelection = Roster.displayMenu (Players)
print ("Exiting Program...")
我一直收到这个错误:
Traceback (most recent call last):
File ".idea/Adv Team Roster.py", line 108, in <module>
Roster.displayRoster ('Players')
File ".idea/Adv Team Roster.py", line 39, in displayRoster
print ("Player's Name:", self.name)
AttributeError: 'str' object has no attribute 'name'
我也遇到了保存/加载例程的问题,但那是另一篇文章。
答案 0 :(得分:3)
您的代码有许多概念性问题需要解决才能开始尝试运行它并开始寻求编译器错误的帮助。这是三个,让你开始。
(1)您的Roster
类旨在实例化,但您永远不会创建实例。每当你写一个课程时,你应该问自己的早期问题是,&#34;什么代码会创建这个类的实例?在什么情况下?该代码将如何获取创建实例所需的数据?该代码在创建实例后立即对该实例做了什么?&#34;
(2)您的Roster
课程名称错误,这可能让您感到困惑。名单是一个球员名单。您的Roster
是关于单个玩家的数据。我建议将Roster
重命名为Player
,创建一些名为Players
的数据结构(如当前的roster
)以容纳一堆玩家,然后跟踪这一变化的后果
(3)一旦你清楚地知道名单和球员是什么,你可以提出类似的问题,&#34;我将在哪里存储名单和球员?我如何将它们传递给我的代码的不同部分?名单应该与哪些功能相关联?与球员?我的代码的顶级?与我还没想过的其他实体一起?&#34;
在完成所有这些思考之后,您可能会得出结论displayPlayer
应该是Player
类的一个函数,因此您需要将displayRoster
重命名为displayPlayer
。既然你已经创建了Player的实例,也许就是一个名为myplayer的实例(这只是一个示例名称),你现在可以说myplayer.displayPlayer()
,Python将运行displayPlayer
代码self
自动设置为myplayer实例。此时,您抱怨的编译器错误将消失,而不是因为您&#34;已修复&#34;它,但是因为一旦你清楚地思考你的系统,它就会自然消失。
总的来说,这是考虑编译器错误的方法:如果不能立即明白如何解决它,那么它可能表明你没有清楚地思考你的系统,所以你需要退后一步,考虑比特定错误更高级别的问题。