I'm very much a noob in programming, and currently world-building for a Sci-Fi based TTRPG. I'm trying to automate generation of various things in my world such as planets etc. One of these is ship generation. I created a class for my ship:
class starShip:
def __init__(self, cost, speed, armour):
self.cost = cost
self.speed = speed
self.armour = armour
And a couple of instances:
shuttle = starShip(200000,3,0)
fighter = starShip(500000,5,5)
I'm using pandas and a csv file to randomise the ships generated. For example:
def shipGenerator(rowNum):
for col in dfShips.columns:
if col == "Ship Type": #need to find relevant column header in the original data frame
result = randint(1,20) #give random number
shipType = dfShips.loc[result,"Ship Type"] #get the value from relevant cell in original data frame
dfGeneratedShips.at[rowNum,col] = shipType #generate the ship type in a new dataframe
Now I can do a lot of if/elif for each type of ship- if shuttle, then put shuttle.cost (instance of the starShip class) in the relevant cost cell etc. etc. But with 12 different types of ships, that's a lot of if/elif, and I'm trying to do this right even though it's a fun project and I'm a complete noob. So my question, is there a way do use a string variable to call an instance? For example, the variable shipType now contains "shuttle" or "fighter". Is there a way to call an instance of starShip class using that variable? Like:
dfGeneratedShips.loc[rowNum,"Cost"] = shipType.cost #place the cost of the relevant ship type under the appropriate column in new dataframe
dfGeneratedShips.loc[rowNum,"Speed"] = shipType.speed
But shipType refers to "shuttle" or "fighter" or "carrier" at each iteration of the loop, so the result changes appropriately.
I hope this was clear enough, appreciate the help!