0

I am getting this error AttributeError: 'str' object has no attribute 'health' I think it is because the object I am trying to modify is a attribute of the instance, but when I refer to the instance I get user input that changes it to a string. How can I change it so that the string refers to the instance instead? I have looked at a post here but it was from 5 years ago and it seems outdated and didn't help.

so in the terminal it shows

Heal who? Rachel # I entered the Rachel part

which is from this part of the code

heal = input("Heal who? ")
Sidekick.heal(heal)

and it executes

    def heal(self,target):
        """Heals the target by 20 hp."""
        if self.level <= 5:
            restore = 20
        elif self.level <= 10:
            restore = 35
        elif self.level <= 15:
            restore = 50
        target.health += restore
        if target.health > target.full_health:
            restore = target.full_health - target.health
            target.health = target.full_health
        print("Healed " + target.name + " by " + str(restore) + " hp.")
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
ewe
  • 159
  • 1
  • 7
  • Why did you think that the string `"Rachel"` has an attribute `health`? What did you expect `target` to be if not a string? – mkrieger1 Aug 27 '20 at 19:28
  • Do you have a variable called `Rachel` that you want to refer to? Then you should store it in a dictionary so that you can look it up by name. – mkrieger1 Aug 27 '20 at 19:34
  • @mkrieger1 I have an instance named Rachel – ewe Aug 27 '20 at 19:57
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – mkrieger1 Aug 27 '20 at 20:18
  • @mkrieger1 no, but your earlier suggestion to make a dictionary works! I made the string the key and the actual instance as the value and it works. Thank you! – ewe Aug 27 '20 at 20:21
  • What I just linked *is* the suggestion to use a dictionary. – mkrieger1 Aug 27 '20 at 20:24

0 Answers0