I'm not even sure I'm calling those by their correct names but here's what I'm working with. I'm working on a simple hockey simulator that only uses math at this time to simulate the results of the games. Currently there is no names or anything like that and I've just generated a "list" of names for "team1" when the scorers are chosen. That's where this comes in. I want to create a list of players for each team and figured functions would be best...if its not please let me know an easier way. So here's the function right now:
def c1():
_name = "Jason Delhomme"
_overall = random.choice((6.00, 7.44, 8.91))
And the _overall variable would need to be called in some places whereas the _name variable would be called in another place. Is this possible?
tm1center = ((c1 * .5) + (c2 * .35) + (c3 * .15)) * 3
Instead of "c1" above I would replace that with _overall from the c1 function. I'm not sure how to do that or if its possible.. sorry for repeating myself here lol...
And then I have this (below) for the player list:
tm1players = ["player1", "player2", "player3", "player4", "player5"]
Ideally, I would replace all of those with the _name variable in each function. The player functions would also go into more depth with their speed, hands, shooting, etc. ratings as well. I don't know how sim sports handle this as I'm very new to coding (just started school) and I wrote the original engine in excel, which is why its just numbers and not physics or plays.
If this doesn't explain it well enough please let me know and I'll further explain what I'm trying to do.
4 Answers 4
In your case, its better to use a class and assign two variable to that class then create instances of it, for example:
class Player:
def __init__(self, name):
self.name = name
self.overall = random.choice((6.00, 7.44, 8.91))
player1 = Player('Jason Delhomme')
player2 = Player('Another Player')
tm1center = (player1.overall * .5) + (player2.overall * .35)
tm1players = [player1.name, player2.name]
EDIT:
to add overall when creating the class you need to change the init method to :
def __init__(self, name, overall):
self.name = name
self.overall = overall
then pass it when creating the class:
player1 = Player('Player Name', 6.23)
You can create a list of players at start and append each instance of player to that list for future use, for example:
player_list = []
player1 = Player('Player Name', 5)
player_list.append(player1)
player2 = Player('Player2 Name', 2.5)
player_list.append(player2)
# dynamically create tm1players and tm1center
tm1players = [x.name for x in player_list]
tm1center = [x.overall for x in player_list]
8 Comments
overall for player2. if you want to add it when creating the player you can add it to __init__() ill edit the answer nowtm1players you can add each class to a list when creating it then use tm1players = [x.name for x in player_list]You have to read about object oriented programming, and then start working with classes, methods, and from that creating objects. This is an example for using different functions (methods) under the umbrella of a Class. Note how the use of self is necessary for accessing variables between the different methods:
ClassHockey():
def __init__(self):
self.description = "in here you would define some 'you always use'
variables"
def c1(self):
self._name = "Jason Delhome"
self._overall = random.choice((6.00, 7.44, 8.91))
def c2(self):
self._new_name = self._name + "Jefferson"
return self._new_name
hockeyName = ClassHockey()
hockeyName.c2()
If you run this you'll get the _new_name "Jason Delhome Jefferson", and you can apply this example to all kinds of operations between your class methods.
Comments
While manually creating a class is a great solution for this problem, I believe that generating one via collections.namedtuple could also work in this senario.
from collections import namedtuple
import random
Player = namedtuple("Player", "name overall")
player1 = Player("Jason Delhomme", random.choice((6.00, 7.44, 8.91)))
player1 = Player("Other Name", random.choice((6.00, 7.44, 8.91)))
final_overall = (player1.overall * .5) + (player2.overall * .35)
Using list comprehension, you could built your entire team:
list_of_players = [Player(input("Enter player: "), random.choice((6.00, 7.44, 8.91))) for i in range(5)]
tm1center = ((list_of_players[0].overall * .5) + (list_of_players[1].overall * .35) + (list_of_players[2].overall * .15)) * 3
2 Comments
collections.namedtuple generates a class for you.Python lets you group info and data together using classes. In your case, you mostly have information about each player, like a name and various stats. You could define a class like:
class Player:
def __init__(self, name):
self.name = name
self.overal = random.choice((6.00, 7.44, 8.91))
Now you can create a bunch of objects from the class:
c1 = Player("player1")
c2 = Player("player2")
c3 = Player("player3")
And you could calculate
tm1center = ((c1.overall * .5) + (c2.overall * .35) + (c3.overall * .15)) * 3
Since you already have a list of player names, you can use a list comprehension to get a list of players in one step:
tm1players = ["player1", "player2", "player3", "player4", "player5"]
tm1 = [Player(name) for name in tm1players]
Now you can access each element in that list, and get their name and overall attributes to do with them as you see fit.
If you want to extend the tm1center1 calculation to include all five players now, you can assign a weight to each one:
class Player:
def __init__(self, name, weight):
self.name = name
self.weight = weight
self.overal = random.choice((6.00, 7.44, 8.91))
tm1center = sum([p.overall * p.weight for p in tm1]) * len(tm1)
where tm1 is defined something like:
tm1players = ["player1", "player2", "player3", "player4", "player5"]
tm1weights = [0.5, 0.3, 0.05, 0.1, 0.05]
tm1 = [Player(name, weight) for name, weight in zip(tm1players, tm1weights)]
Python is a pretty flexible language. Go crazy with it!
classinstead of functions. Then make the specific players instances of that class.