+ 3
Take input and make it instance of a class in Python.
Suppose I take input of 11 football players and make each player an instance of class Players. How do I do it?
4 Answers
+ 2
This will create a list 'team' with all players as objects. You can call every player by his number (e.g. team[4] is player 4).
________________
class player:
def __init__ (self, name):
self.name = name
team = []
for i in range(1, 12):
name = input ('Name of player {}:'.format(i))
team.append (player (name))
for i in range (len(team)):
print (team[i].name)
+ 3
Sebastian KeĂler - great example!
+ 2
Maybe this is a helpful example?
https://code.sololearn.com/cYdewRD1001I/?ref=app
+ 2
i wanted to access players by their names. so instead of array I created a dictionary with key as the player's name and value as class object. works perfect. thank you!