In: Computer Science
Python Please
Define a class that will represent soccer players as objects. A soccer player will have as attributes, name, age, gender, team name, play position on the field, total career goals scored. The class should have the following methods:
1. initializer method that will values of data attributes arguments. Use 0 as default for career goals scored.
2. str method to return all data attributes as combined string object.
3. addToGoals that will accept an argument of int and add to the career goals scored.
4. provide mutators for age, play position on the field, and team name.
5. provide an accessor that will return all data attributes as a tuple.
Program


Text Format:
class SoccerPlayer:
def __init__(self, name, age, gender, team_name, position, goals=0):
# Constructor to initialize data attributes
self.name = name
self.age = age
self.gender = gender
self.team_name = team_name
self.play_position = position
self.career_goals = goals
def __str__(self):
# return all data attributes as combined string object.
return 'Name:' + self.name + ', age:' + str(self.age) + ', gender:' + self.gender + \
', team name:' + self.team_name + ', play position:' + self.play_position + \
', career goals:' + str(self.career_goals)
def addToGoals(self, goals):
# add to the career goals scored.
self.career_goals += goals
def set_age(self, age):
# mutator for age
self.age = age
def set_play_position(self, position):
# mutator for play_position
self.play_position = position
def set_team_name(self, team):
# mutator for team_name
self.team_name = team
def __iter__(self):
# accessor return all data attributes as a tuple
yield from self.name, self.age, self.gender, self.team_name, self.play_position, self.career_goals
p = SoccerPlayer('John', 23, 'male', 'AC Milan', 'Forward', 34)
name, age, gender, team, position, goals = p
p.addToGoals(3)
print(p)
print(tuple(p))
Output:
Name:John, age:23, gender:male, team name:AC Milan, play
position:Forward, career goals:37
('John', 23, 'male', 'AC Milan', 'Forward', 37)
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.