Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to edit the self.TeamScore value in my code

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading