Sunday, 15 September 2013

Python - Instancing classes with a for loop -


I'm having trouble doing so:

  get started def _ Player (num_players): player_list = [] I (num_players) in category: player_list.append (player ("none"))   

But once I add a variable to it I do not get anything:

  def instance_ports (port_list): all_ports = [] I in port_list: all_ports.append (port (i))   

I know that there is a way to do this. Sorry, still new

Now player_list and all_ports < / Code> are local variables That is, they are only available within the work in which they are prescribed. Any attempt to use them outside of the function will fail.

Try to do your work

  Launch DEF _play_player: player_list = [] I (num_players) in category: Player_list.append (Player (" None) ") player_list Return   

and

  def instance_ports (port_list): all_ports = [] in Port_list for: all_ports.append (port (I) returns all_ports   

Now you can call them from your preference:

  num = 5 Port_list = [1, 3,5,7] player_list = initial_players (num) port_list = instance_ports (ports)   

Keep in mind, however, call these multiple times Mitigation that is not cumulative effects, even though it appears you create them plan to call time.

No comments:

Post a Comment