DawnLeigh
DawnLeigh

Reputation: 11

Team Roster = Remove & Edit player in Python 3

I am new to programming and am learning OOP and dictionaries. I have an assignment for which "the program utilizes object-oriented programming to create a member object which can accept the player’s name, phone number, and jersey number. You will need to replace the list with a dictionary to help in locating objects for removal and editing." Here's my code so far:

class Roster:

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("Player's Information")
    print("-------------------")
    print("Player's Name:", self.__name)
    print("Player's Telephone number:", self.__phone)
    print("Player's Jersey number:", self.__number)

def displayMenu():
    print("==========Selection Menu==========")
    print("1. Add a Player to the Roster")
    print("2. Remove a Player from the Roster")
    print("3. Change a Player Name displayed in the Roster")
    print("4. Quit")
    print()
    return int(input("Selection>"))

def addPlayer():
    newName = input("Add a player's Name: ")
    newPhone = input("Telephone number: ")
    newNumber = input("Jersey number: ")
    players = {}
    players[newName] = newName, newPhone, newNumber

def removePlayer():
    removeName = input("What name would you like to remove? ")
    players = {}
    if removeName in players:
        del players[removeName]
    else:
        print("Sorry", removeName, "was not found!")

def editPlayer():
    oldName = input("What name would you like to change? ")
    players = {}
    if oldName in players:
        newName = input("What is the new name? ")
        print("***", oldName, "has been changed to", newName)
    else:
        print("***Sorry", oldName, "was not found!")

However, when I try to remove player or edit a player I get:

Selection>1

Add a player's Name: Dawn

Telephone number: 555-5555

Jersey number: 12

==========Selection Menu==========

  1. Add a Player to the Roster

  2. Remove a Player from the Roster

  3. Change a Player Name displayed in the Roster

  4. Quit

Selection>2

What name would you like to remove? Dawn

Sorry Dawn was not found!

==========Selection Menu==========

  1. Add a Player to the Roster

  2. Remove a Player from the Roster

  3. Change a Player Name displayed in the Roster

  4. Quit

Selection>3

What name would you like to change? Dawn

***Sorry Dawn was not found!

What am I doing wrong here?

Upvotes: 1

Views: 6962

Answers (1)

Kush
Kush

Reputation: 1036

Morgan already commented what was going wrong, but I thought I would expand on it a little bit more since you mentioned you were just starting out programming.

Say we are on step 1 of your input and we call addPlayer()

def addPlayer():
    newName = input("Add a player's Name: ")
    newPhone = input("Telephone number: ")
    newNumber = input("Jersey number: ")
    players = {}
    players[newName] = newName, newPhone, newNumber

The first three lines are just going to grab the information you insert from the command line, it looks fine. The important line is next where you have players = {}, which is initializing a dictionary named players. The important thing to realize here is that you are creating a new dictionary every time this code is ran. That means whenever you call addPlayers, you are not actually adding a player, you are defining a new dictionary named players and adding a name to it.

There is also the problem that the players that you define in addPlayer never leaves the addPlayer method. Because you create players in every single method (the players = {} line), you are never actually saving any of the data that is created when you call addPlayer().

Ideally, you would want something like this:

players = {} # One definition used by all methods

def addPlayer(players): # players as an argument
    newName = input("Add a player's Name: ")
    newPhone = input("Telephone number: ")
    newNumber = input("Jersey number: ")
    # Don't redefine it!
    players[newName] = newName, newPhone, newNumber

def removePlayer(players): # players as an argument
    removeName = input("What name would you like to remove? ")
    # Don't redefine it!
    if removeName in players:
        del players[removeName]
    else:
        print("Sorry", removeName, "was not found!")

# rest of code adjusted

This would work and allow you to add/remove/edit players, but it is not what your assignment wants you to do. Instead, lets read what your assignment specifically says.

the program utilizes object-oriented programming to create a member object which can accept the player’s name, phone number, and jersey number

So, what is a member object then? If you have no clue, check out the python documentation on classes. It should give you enough information to know exactly what the assignment is asking you to do. Pay close attention to the "Class Objects" section, as it has some code samples that could really help you transfer your current code to a OOP format.

Upvotes: 1

Related Questions