Reputation: 31
I am trying to use a for loop to create 5 (currently) variables. I want to make these variables be named soldier+i, where i is the number iterated in the for loop.
first_names=['name1', 'name2', 'name3', 'name4', 'name5']
last_names=['Lname1', 'Lname2', 'Lname3', 'Lname4', 'Lname5']
nick_names=['1','2','3','4','5']
soldier_HPs=[10,9,8,7,6]
for i in range(5):
soldier+i=[first_names[i-1],last_names[i-1],nick_names[i-1],soldierHPs[i-1]]
As you can see, I want to store each soldier by naming the soldier "soldier+(respective number)". Then store its health, first, last, and nick names all as part of a list. The issue i am having is that it cannot concantenate strings and integers. the issue is that this is supposed to be the name of the variable. If I only wanted this to be a string to set a variable to, i could just use: x="soldier" + str(i). But again since this is the name of a variable, this will not work.
Upvotes: 1
Views: 988
Reputation: 2687
This may be a little too advanced for you right now, but I think it's good to understand the concepts of object oriented programming and get introduced to them as soon as possible. A possible solution for this would be the creation of a Soldier class
, like so:
class Soldier(object):
def __init__(self,first_name,last_name,nickname,hp):
self.first_name = first_name
self.last_name = last_name
self.nickname = nickname
self.hp = hp
def __repr__(self):
return str(self.nickname)
soldier_one = Soldier('name1','lname1','1',10)
print soldier_one ## 1
print soldier_one.first_name ## name1
print soldier_one.hp ## 10
soldier_two = Soldier('name2','lname2','2',9)
and so on. Let me know if you have any questions about this approach - it's a little less intuitive at first, but I seriously wished that I had taken it more seriously when I started off in Python.
Upvotes: 0
Reputation: 2158
Welcome to Stackoverflow! I notice your profile as a high school student, so I won't assume any prior knowledge from you. Let's get to your questions then.
You are using list
s in Python to store information about soldiers. And then you want to store information for each soldier, separately. Now, isn't that quite redundant?
If I want the first name of the first soldier, I can simply go for first_name[0]
. All information is accessible in the current scope through meaningful variable names (i.e. first_names, last_names etc). If you put them in a list, you have to remember the first index is first_names
, second is last_names
, etc. These add another layer of confusion as your program/game code grows larger.
However, in some cases it is indeed more convenient to group information about the characters for purpose of iterations. In Python there is module called collections
, where you can find a function called namedtuple
. This function is a so called factory function
, whose sole purpose is to declare and define class for you.
Say I want to define a Soldier
class quickly, here is how
from collections import namedtuple
Soldier = namedtuple('Soldier', ['first_name', 'last_name', 'nick_name', 'HP'])
soldier_info = [ # this is list comprehension
Soldier(first_names[idx], last_names[idx], nick_names[idx], soldier_hps[idx])
for idx in range(len(first_names))
]
Then you can do
firstname_second_soldier = soldier_info[1].first_name
namedtuple
is immutable and hashable like tuple
, but with named fields.
Upvotes: 1
Reputation: 176
Why not use a dictionary?
first_names=['name1', 'name2', 'name3', 'name4', 'name5']
last_names=['Lname1', 'Lname2', 'Lname3', 'Lname4', 'Lname5']
nick_names=['1','2','3','4','5']
soldier_HPs=[10,9,8,7,6]
soldiers = {}
for i in range(5):
soldiers['soldier' + str(i-1)] = (first_names[i-1],last_names[i-1],nick_names[i-1],soldier_HPs[i-1])
Soldiers will then have the value
{'soldier2': ('name3', 'Lname3', '3', 8), 'soldier3': ('name4', 'Lname4', '4', 7), 'soldier0': ('name1', 'Lname1', '1', 10), 'soldier1': ('name2', 'Lname2', '2', 9), 'soldier-1': ('name5', 'Lname5', '5', 6)}
Upvotes: 0