0

I'm in my very first programming class and for whatever reason I CANNOT fix this code. We have to write a program to hold a roster. My teacher has given us a template and I stuck to it exactly. She isn't the best at responding to emails and I have searched the internet over trying to figure it out. I've checked my indentations a million times so I know thats not it. I'll include the entire code below, thanks for helping!

class TeamClass(object):
name = ""
jersey = ""
number = ""

def __init__(self, name, jersey, number):
    self.name = name
    self.jersey = jersey
    self.number = number

def set_name(self, name):
    self.name = name

def set_jersey(self, jersey):
    self.jersey = jersey

def set_number(self, number):
    self.number = number

def get_name(self):
    return self.name

def get_jersey(self):
    return self.jersey

def get_number(self):
    return self.number

def display_team_roster(self):
    print("Player")
    print("Name: ", self.name)
    print("Jersey: ", self.jersey)
    print("Phone Number: ", self.number)
    print("-------------")

def menu(self):
    print("")
    print("<<<<<Main Menu>>>>>")
    print("1. Display Team Roster")
    print("2. Add Team Member")
    print("3. Remove Team Member")
    print("4. Edit Team Member")
    print("9. Exit Program")
    selection = int(input("Enter a menu number or 9 to exit: "))
    print("")
    return selection

def display_roster(roster):
    if len(roster) > 0:
        for x in roster.keys():
            roster[x].display_team_roster()
    else:
        print("Roster is empty")

def add_member(roster):
    new_member = input("Enter new member: ")
    new_jersey = input("Enter jersey#: ")
    new_number = input("Enter phone number: ")
    roster[new_member] = TeamClass(new_member, new_jersey, new_number)
    return roster

def del_member(roster):
    del_member = input("Which member would you like to remove?: ")
    if del_member in roster:
        del roster[del_member]
        print(del_member, "has been removed")
    else:
        print(del_member, "was not found")
        return roster

def edit_member(roster):
    existing_member = input("Which member would you like to edit? ")
    if existing_member in roster:
        new_member = input("Enter new members name: ")
        new_jersey = input("Enter new jersey#: ")
        new_number = input("Enter new phone number: ")
        roster[existing_member] = TeamClass(new_member, new_jersey, new_number)
        print(existing_member, "has been changed to", new_member)
    else:
        print(existing_member, "was not found")
        return roster
print("")
roster = {}
menu_selection = menu(object)
while menu_selection != 9:
    if menu_selection == 1:
        display_roster(roster)
    elif menu_selection == 2:
        roster = add_member(roster)
    elif menu_selection == 3:
        roster = del_member(roster)
    elif menu_selection == 4:
        roster = edit_member(roster)
        menu_selection = menu()
print("Exiting program")
2
  • 2
    Is that how your code is indented? Python uses indentation in place of {} so you'd want to indent the member functions to ensure they were parsed as part of the class definition. Commented Nov 16, 2019 at 5:31
  • everything in the class needs to be indented Commented Nov 16, 2019 at 5:38

3 Answers 3

1

Python is strict about indenting so in order to make this work correct your class with following indentation:

class TeamClass(object):
    name = ""
    jersey = ""
    number = ""
Sign up to request clarification or add additional context in comments.

Comments

1

You probably need to 4-space indent all lines below the class TeamClass(object) line until the def menu(self): line since all of those functions seem to be TeamClass's methods. Although menu(self) might seem like a class method since it has self in the argument but I don't think that was the case since menu was called directly on line menu_selection = menu(object)

class TeamClass(object):
    name = ""
    jersey = ""
    number = ""

    def __init__(self, name, jersey, number):
        self.name = name
        self.jersey = jersey
        self.number = number

    def set_name(self, name):
        self.name = name

    def set_jersey(self, jersey):
        self.jersey = jersey

    def set_number(self, number):
        self.number = number

    def get_name(self):
        return self.name

    def get_jersey(self):
        return self.jersey

    def get_number(self):
        return self.number

    def display_team_roster(self):
        print("Player")
        print("Name: ", self.name)
        print("Jersey: ", self.jersey)
        print("Phone Number: ", self.number)
        print("-------------")

def menu(self):
    print("")
    print("<<<<<Main Menu>>>>>")
    print("1. Display Team Roster")
    print("2. Add Team Member")
    print("3. Remove Team Member")
    print("4. Edit Team Member")
    print("9. Exit Program")
    selection = int(input("Enter a menu number or 9 to exit: "))
    print("")
    return selection

def display_roster(roster):
    if len(roster) > 0:
        for x in roster.keys():
            roster[x].display_team_roster()
    else:
        print("Roster is empty")

def add_member(roster):
    new_member = input("Enter new member: ")
    new_jersey = input("Enter jersey#: ")
    new_number = input("Enter phone number: ")
    roster[new_member] = TeamClass(new_member, new_jersey, new_number)
    return roster

def del_member(roster):
    del_member = input("Which member would you like to remove?: ")
    if del_member in roster:
        del roster[del_member]
        print(del_member, "has been removed")
    else:
        print(del_member, "was not found")
        return roster

def edit_member(roster):
    existing_member = input("Which member would you like to edit? ")
    if existing_member in roster:
        new_member = input("Enter new members name: ")
        new_jersey = input("Enter new jersey#: ")
        new_number = input("Enter new phone number: ")
        roster[existing_member] = TeamClass(new_member, new_jersey, new_number)
        print(existing_member, "has been changed to", new_member)
    else:
        print(existing_member, "was not found")
        return roster

print("")
roster = {}
menu_selection = menu(object)
while menu_selection != 9:
    if menu_selection == 1:
        display_roster(roster)
    elif menu_selection == 2:
        roster = add_member(roster)
    elif menu_selection == 3:
        roster = del_member(roster)
    elif menu_selection == 4:
        roster = edit_member(roster)
        menu_selection = menu()
print("Exiting program")

1 Comment

Thank you so much! It worked, I'm trying so hard to understand this but it's just not clicking for me!
0

You missed a spaces there you need to add spaces in your name and jersey and number

Try This:

class TeamClass(object):
    name = ""
    jersey = ""
    number = ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.