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

25th Feb 2018, 7:08 PM
Aditya Rana
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)
25th Feb 2018, 7:41 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 3
Sebastian Keßler - great example!
1st May 2018, 8:56 PM
Johannes
Johannes - avatar
+ 2
Maybe this is a helpful example? https://code.sololearn.com/cYdewRD1001I/?ref=app
25th Feb 2018, 7:44 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 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!
1st Mar 2018, 8:25 PM
Aditya Rana