0

In my program I define a survivor class below and instance it.

class survivor(object) :
    def __init__(self, fname, sname, age, currhealth, maxhealth, currwill, maxwill,currspeed,maxspeed, ability):
        self.fname = fname
        self.sname = sname
        self.age = age
        self.health = currhealth
        self.maxhealth = maxhealth
        self.will = currwill
        self.maxwill = maxwill
        self.speed = currspeed
        self.maxspeed = maxspeed
        self.ability = ability

Samuel = survivor("Samuel","Jordie",17,6,6,2,2,4,4,"Strength")
Marie = survivor("Marie","Wems",16,4,4,5,5,4,4,"Heal")
Cumbo = survivor("Cumbo","Chan",17,3,3,4,4,3,3,"Consume")
Sinead = survivor("Sinead","Mess",17,5,5,5,5,6,6,"Tools")
Maria = survivor("Maria","Wiks",16,4,4,5,5,2,4,"Eat")

The user chooses 4 survivors and a list of the names of these survivors is created for example

chosen_survivors = ["Samuel","Maria","Cumbo","Sinead"]

I then want to present some attributes of the chosen survivor to the user, irrespective of which survivor they choose.

E.g

print(chosen_survivors[0].health)

How do I achieve this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Ripseed Oil
  • 77
  • 1
  • 1
  • 8

1 Answers1

2

You're going to want to use a dictionary.

survivors = {
  "Samuel": survivor(...),
  "Marie": survivor(...),
  ...
}

chosen = ["Samuel", "Marie", ...]

print(survivors[chosen[0]].health)  # prints Samuel's health
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25