Python Forum
'function' object has no attribute 'health'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'function' object has no attribute 'health'
#1
How do I fix this? (PY Latest version)
class Character(object):
    def __init__(self):
        self.health = 100

class Blacksmith(Character):
    def __init__(self):
        super(Blacksmith, self).__init__()


bs = Blacksmith
print (bs.health)
Here's the error:
  File "classes_01.py", line 11, in <module>
    print (bs.health)
AttributeError: type object 'Blacksmith' has no attribute 'health'











Another one I got is this
class Mage:
    '''
    A mage that visited a shady magic shop
    '''
    
    
    
    def David(David,health,mana):
        David = Mage("David")
        David.health = 100
        David.mana = 90
        
    iBuy = input("Shady seller: What would you like to buy? \n\nApple \n\nDagger \n\nHealth potion \n\nMana potion \n\n  ")
    if (iBuy == 'Apple'):
            print("\nThe apple was posionous! \nThe seller tricked me!")
            David.health -= 90
            print("-The mage puked-" "\nI knew I couldn't trust him!")
    elif (iBuy == 'Dagger'):
            print("2 Days later")
            
            myWeapon = input('A thief broke in your house, because he wasnt quiet enough, you caught him. \n\nWhich weapon would you use to fight him? : \n\nStaff \n\nDagger \n   ')        
                
            if (myWeapon == 'Dagger'):
                    print ('The dagger the shady shop seller gave you broke!')
                    David.health -= 50
                    print(David.health)
            elif (myWeapon == 'Staff'):
                print("You beat the filthy thief, you selled his loot to buy a health potion")
                David.health +10
                print (David.health)
    elif (iBuy == 'Health potion'):
            David.health +=5
            print("The potion was weak, yet added bonus HP \n\nThe potion was really expensive considering the health it gave.")
    elif (iBuy == 'Mana potion'):
        David.mana += 20
        print ("The mana potion was the only reliable thing to buy in this damn shop!")
        print (David.mana)
The ERROR:
  File "magegame.py", line 1, in <module>
    class Mage:
  File "magegame.py", line 25, in Mage
    David.health -= 50
AttributeError: 'function' object has no attribute 'health'
Reply
#2
bs = Blacksmith() # the parentesis
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Apr-12-2017, 05:30 PM)wavic Wrote:
bs = Blacksmith() # the parentesis
Helped!
Appreaciate it. Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,788 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,970 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 764 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,422 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,752 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,994 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,569 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,201 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,476 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,939 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020