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 can I select a variable based on a number that the user inputs

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)

>Solution :

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

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