Home > Software engineering >  How to edit the self.TeamScore value in my code
How to edit the self.TeamScore value in my code

Time:03-29

I am trying to make a leaderboard for a school tournament. I began asking the user to input some team names and how many members in the team. Now i want to be able to ask the user; 'who won the game?' and then adjust that teams score by 1.

How can i change a teams score based on the users input?

class AllTeams:
  def __init__(self, TeamNum, TeamName, TeamMembers,    TeamScore):
    self.TeamNum = TeamNum
    self.TeamName = TeamName
    self.TeamMembers = TeamMembers
    self.TeamScore = TeamScore

  def __repr__(self):
    return f'Team Number: {self.TeamNum} |-| Team Name: {self.TeamName} |-| Member Count: {self.TeamMembers} |-| Team Score: {self.TeamScore}'

#teams = [AllTeams(i 1, "N/A", 0) for i in range(20)]
teams = []

TeamCounter=int(input('How many Teams will be in the tournament? '))

print('')
for i in range(TeamCounter):
    NameOfTeam=input(f'Please Enter Team {i 1} Name: ')
    MemberCount=input('How Many Members in Team? ')
    print('')
    teams.append( AllTeams( i 1, NameOfTeam, MemberCount, 0) )

def score():
    for t in teams:
    print(t)

    GameWinner=input('Which Team Won the Event? ')

    #change team score by 1

CodePudding user response:

You need to iterate on the teams to find the one with the good name to update hi score

def score(teams):
    winner = input('Which Team Won the Event? ')
    for team in teams:
        if team.name == winner:
            team.add_victory()
            break

Then a few better naming and the class becomes

class Team:
    def __init__(self, num, name, size, score):
        self.num = num
        self.name = name
        self.size = size
        self.score = score

    def add_victory(self):
        self.score  = 1

    def __repr__(self):
        return f'Team Number: {self.num} |-| Team Name: {self.name} |-| Member Count: {self.size} |-| Team Score: {self.score}'

CodePudding user response:

Thinking through your direction here, I think it helps to clear up some misunderstandings on classes(or what I and others are perceiving as misunderstandings).

In short, a class is an object, which is like a representation of a thing. In your case a Team is a thing. It has a count of members, a name, a number, etc. So your class should be named "Team" not "AllTeams".

In that same vein, a Game is a thing. It has attributes like a home team, an away team, a date on which it was played, a score for the home and away team, a venue, etc. And it may have a method like "WhoWon()" that uses the score to determine whether the home team or away team won.

Thinking through that it makes sense that a score is an attribute of a game, not a team, since it's likely a team will play multiple games during the tournament and will have a distinct score for each game played. That would be a pain to track inside a Team.

So, consider something like:

class Team:

    #setting this outside of a method allows us to get it or set it outside of the class in the main code like a "global" for the class.
    TeamName = ''

    def __init__(self, TeamNum, TeamName, TeamMembers):
        self.TeamNum = TeamNum
        self.TeamName = TeamName
        self.TeamMembers = TeamMembers

    def __repr__(self):
        return f'Team Number: {self.TeamNum} |-| Team Name: {self.TeamName} |-| Member Count: {self.TeamMembers} |-| Team Score: {self.TeamScore}'

class Game:
    
    def __init__(self, game_number, home_team, away_team, home_team_score, away_team_score):
        self.home_team = home_team
        self.away_team = away_team
        self.home_team_score = home_team_score
        self.away_team_score = away_team_score

    #a class method that returns the winner based on the scores
    def winner(self):
        if self.home_team_score > self.away_team_score:
            return self.home_team
        else:
            return self.away_team

#instead of lists, use dictionaries so you can refer to a game by its number, or a team by its name without having to jump through hoops. 
games={}
teams = {}

TeamCounter=int(input('How many Teams will be in the tournament? '))

print('')
for i in range(TeamCounter):
    NameOfTeam=input(f'Please Enter Team {i 1} Name: ')
    MemberCount=input('How Many Members in Team? ')
    print('')
    #Add to the teams dictionary.
    teams[NameOfTeam] = Team( i 1, NameOfTeam, MemberCount) 


#Lets collect info about the games in the tourny:
GameCounter=int(input("How many games are in the tournament?"))
for i in range(GameCounter):
    home_team = input("what was the name of the home team?")
    home_team_score = input("what was their score?")
    away_team = input("what was the name of the away team?")
    away_team_score = input("what was their score?")

    #create the game object
    this_game = Game(i, teams[home_team], teams[away_team], home_team_score, away_team_score)

    #Once we have this one game made we can see who won. Note that we call the method `winner()` for `this_game` object which returns a `Team` object for which we can get it's attribute `TeamName`: 
    print("It looks like " this_game.winner().TeamName " won the game!")

There's a million ways to pull this off, but I feel like this will get you going in the right direction.

  • Related