Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a "variable" from a function in Python?

Tags:

python

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.

like image 259
Robmeister89 Avatar asked Dec 31 '25 17:12

Robmeister89


1 Answers

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]
like image 110
Mohd Avatar answered Jan 03 '26 07:01

Mohd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!