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
>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}'