I am trying to change the data in classes that I have created. I have created 20 data sets (t1-20) with an empty field for the team name and score I want to ask the user which team name/number they would like to edit, and then add their input into the list from the class 'AllTeams'
class AllTeams:
def __init__(self, TeamNum, TeamName, TeamScore):
self.TeamNum = TeamNum
self.TeamName = TeamName
self.TeamScore = TeamScore
def myfunc(abc):
print('Team Number:',abc.TeamNum,'|-|', 'Team Name:',abc.TeamName, '|-|', 'Team Score:',abc.TeamScore)
t1 = AllTeams(1, "N/A", 0)
t2 = AllTeams(2, "N/A", 0)
t3 = AllTeams(3, "N/A", 0)
t4 = AllTeams(4, "N/A", 0)
t5 = AllTeams(5, "N/A", 0)
t6 = AllTeams(6, "N/A", 0)
t7 = AllTeams(7, "N/A", 0)
t8 = AllTeams(8, "N/A", 0)
t9 = AllTeams(9, "N/A", 0)
t10 = AllTeams(10, "N/A", 0)
t11 = AllTeams(11, "N/A", 0)
t12 = AllTeams(12, "N/A", 0)
t13 = AllTeams(13, "N/A", 0)
t14 = AllTeams(14, "N/A", 0)
t15 = AllTeams(15, "N/A", 0)
t16 = AllTeams(16, "N/A", 0)
t17 = AllTeams(17, "N/A", 0)
t18 = AllTeams(18, "N/A", 0)
t19 = AllTeams(19, "N/A", 0)
t20 = AllTeams(20, "N/A", 0)#data for teams
t1.myfunc()
t2.myfunc()
t3.myfunc()
t4.myfunc()
t5.myfunc()
t6.myfunc()
t7.myfunc()
t8.myfunc()
t9.myfunc()
t10.myfunc()
t11.myfunc()
t12.myfunc()
t13.myfunc()
t14.myfunc()
t15.myfunc()
t16.myfunc()
t17.myfunc()
t18.myfunc()
t19.myfunc()
t20.myfunc()#printing the leaderboard of teams
TeamCounter=int(input('How many Teams will be in the tournament? '))
conversion=str(TeamCounter)
print('')
while TeamCounter>0:
NameOfTeam=input('Please Enter Team Name: ')
MemberCount=input('How Many Members in Team? ')
TeamCounter-=1 #need this for the loop to work
NewTeam=('t' conversion)
print(NewTeam)
CodePudding user response:
This is a summary of all of the comments so far. Note that it's silly to create 20 teams, and then ask how many there will be. This only creates the teams they ask for. Also, you didn't have a place to put the number of players, so I'm not storing that.
class AllTeams:
def __init__(self, TeamNum, TeamName, TeamScore):
self.TeamNum = TeamNum
self.TeamName = TeamName
self.TeamScore = TeamScore
def __repr__(self):
return f'Team Number: {self.TeamNum} |-| Team Name: {self.TeamName} |-| 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? ')
teams.append( AllTeams( i 1, NameOfTeam, 0 ) )
for t in teams:
print(t)
Output:
C:\tmp>python x.py
How many Teams will be in the tournament? 8
Please Enter Team 1 Name: abc
How Many Members in Team? 3
Please Enter Team 2 Name: def
How Many Members in Team? 3
Please Enter Team 3 Name: ghi
How Many Members in Team? 2
Please Enter Team 4 Name: jkl
How Many Members in Team? 9
Please Enter Team 5 Name: mno
How Many Members in Team? 3
Please Enter Team 6 Name: pqr
How Many Members in Team? 3
Please Enter Team 7 Name: stu
How Many Members in Team? 4
Please Enter Team 8 Name: vwx
How Many Members in Team? 3
Team Number: 1 |-| Team Name: abc |-| Team Score: 0
Team Number: 2 |-| Team Name: def |-| Team Score: 0
Team Number: 3 |-| Team Name: ghi |-| Team Score: 0
Team Number: 4 |-| Team Name: jkl |-| Team Score: 0
Team Number: 5 |-| Team Name: mno |-| Team Score: 0
Team Number: 6 |-| Team Name: pqr |-| Team Score: 0
Team Number: 7 |-| Team Name: stu |-| Team Score: 0
Team Number: 8 |-| Team Name: vwx |-| Team Score: 0
C:\tmp>
CodePudding user response:
Your class is badly named because it only represents a single team - so I've renamed it.
I've added a repr for ease of printing.
There are a fixed number of teams. As someone else has said you should think about asking the user how many teams they want but I'll leave it as fixed for the purposes of demonstration.
class Team:
def __init__(self, TeamNum, TeamName, TeamScore):
self.TeamNum = TeamNum
self.TeamName = TeamName
self.TeamScore = TeamScore
def __repr__(self):
return f'Team number: {self.TeamNum} |-| Team name: {self.TeamName} |-| Team score: {self.TeamScore}'
# construct a list of 20 teams
teams = [Team(n 1, 'N/A', 0) for n in range(20)]
# loop indefinitely until user inputs zero
while True:
tnum = input('Enter team number (0 to end): ')
try:
# make sure the input is a number and also within range
if (tnum := int(tnum)) == 0:
break
if tnum < 1 or tnum > len(teams):
raise ValueError
tname = input('Enter team name: ')
# assign the input team name
teams[tnum-1].TeamName = tname
# print the modified team class
print(teams[tnum-1])
except ValueError:
print(f'You need to enter a number between 1 and {len(teams)}')
# print all the teams
for team in teams:
print(team)