AttributeError:'元组'对象没有属性python错误

时间:2018-04-22 17:09:40

标签: python class printing display attributeerror

我试过一遍又一遍地运行这段代码,但无法弄清楚问题。添加名称时,结果可以正常显示。当我编辑名称然后尝试显示名单时,我收到错误。错误是:

Traceback (most recent call last):
  File "Z:/Grantham University/programming essentials/Week 5 Cody Pillsbury.py", 
line 88, in <module>

DisplayTeamRoster(teamRoster)
  File "Z:/Grantham University/programming essentials/Week 5 Cody Pillsbury.py", 
line 39, in DisplayTeamRoster
   teamRoster[x].displayTeamRoster()
AttributeError: 'tuple' object has no attribute 'displayTeamRoster'

以下是代码:

import sys;

class Roster:
    name = ""
    phoneNumber = ""
    jersyNumber = 0
    def __init__(self, name, phoneNumber, jersyNumber):
        self.name = name
        self.phoneNumber = phoneNumber
        self.jersyNumber = jersyNumber

    def setname(self, name):
        self.name = name
    def setphoneNumber(self, phoneNumber):
        self.phoneNumber = phoneNumber
    def setjersyNumber(self, jersyNumber):
        self.jersyNumber = jersyNumber

    def getname(self):
        return self.name
    def getphoneNumber(self):
        return self.phoneNumber
    def getjersyNumber(self):
        return self.jersyNumber

    def displayTeamRoster(self):
        print("Member information:")
        print("-------------------")
        print("Name:", self.name)
        print("Phone Number:", self.phoneNumber)
        print("Jersy Number:", self.jersyNumber)


def DisplayTeamRoster(teamRoster):
        if len(teamRoster) == 0:
            print("There are no players on this team!")
        else:
            for x in teamRoster.keys():
                teamRoster[x].displayTeamRoster()
def addPlayer(teamRoster):
        playerName = input("Please Enter Player\'s Name:")
        playerPhoneNumber = input("Please Enter Player\'s Phone Number:")
        playerJersyNumber = int(input("Please Enter Player\'s Jersy Number:"))

        teamRoster[playerName] = Roster(playerName, playerPhoneNumber, playerJersyNumber)
        print(playerName, "is now on the Team Roster!")
        return teamRoster
def removePlayer(teamRoster):
        removed = input("Please Enter Player You Would Like To Remove:")
        if removed in teamRoster:
                del teamRoster[removed]
                print(removed, "is no longer on in the team roster!")
        else:
                print(removed, "was not found in the team roster!")
        return teamRoster
def editPlayer(teamRoster):
        oldName = input("Please enter the player\'s name you would like to change:")
        if oldName in teamRoster:
            playerName = input("What is the player\'s new name:")
            playerPhoneNumber = input("What is the player\'s new phone number:")
            playerJersyNumber = input("What is the player\'s new jersey number:")
            teamRoster[oldName] = (playerName, playerPhoneNumber, playerJersyNumber)
        else:
            print(oldName, "was not found in the team roster!")
        return teamRoster
def exitProgram():
        print("Exiting Program......")
        sys.exit()
def mainApp():
        print("---------Main Menu---------")
        print("(1) Display Team Roster")
        print("(2) Add Player")
        print("(3) Remove Player")
        print("(4) Edit Player")
        print("(9) Exit Program")

        try:
            test = int(input("Please choose a menu option:"))
        except ValueError:
            print("invalid input, you must use corresponding number in menu")
        else:
            return test

print("Welcome to the Team Manager")
teamRoster = {}
options = mainApp()
while options != 9:
    if options == 1:
        DisplayTeamRoster(teamRoster)
    elif options == 2:
        teamRoster = addPlayer(teamRoster)
    elif options == 3:
        teamRoster = removePlayer(teamRoster)
    elif options == 4:
        teamRoster = editPlayer(teamRoster)
    elif options == 9:
        exitProgram()
    else:
        print("invalid selection")
    options = mainApp()

2 个答案:

答案 0 :(得分:0)

editRoster()

中的这行代码
teamRoster[oldName] = (playerName, playerPhoneNumber, playerJersyNumber)

正在用Roster()替换teamRoster对象3-tuple。当您在该元组上调用displayRoster时,这会产生问题。

您需要修改Roster()对象到位

teamRoster[oldName].playerName = playerName
teamRoster[oldName].platerPhoneNumber = playerPhoneNumber
teamRoster[oldName].platerJersyNumber = playerJersyNumber

然后你保持相同的对象,一切都能正常使用更新的值。

答案 1 :(得分:0)

我的问题是editPerson方法中的拼写错误。我不得不改变:

def editPlayer(teamRoster):
    oldName = input("Please enter the player\'s name you would like to change:")
    if oldName in teamRoster:
        playerName = input("What is the player\'s new name:")
        playerPhoneNumber = input("What is the player\'s new phone number:")
        playerJersyNumber = input("What is the player\'s new jersey number:")
        teamRoster[oldName] = (playerName, playerPhoneNumber, playerJersyNumber)
    else:
        print(oldName, "was not found in the team roster!")
    return teamRoster

def editPlayer(teamRoster):
    oldName = input("Please enter the player\'s name you would like to change:")
    if oldName in teamRoster:
        playerName = input("What is the player\'s new name:")
        playerPhoneNumber = input("What is the player\'s new phone number:")
        playerJersyNumber = input("What is the player\'s new jersey number:")
        teamRoster[oldName] = Roster(playerName, playerPhoneNumber, playerJersyNumber)
    else:
        print(oldName, "was not found in the team roster!")
    return teamRoster

我需要做的就是放入名册,这样玩家就可以插入到Team Roster词典中。