Working on classes for a text based game in python


i'm trying to create a function to raise max hp whenever a stat is increased or decreased. i have tried changing self.maxHpCalc() player.** moving the variables = (int) into the orc class in every why i can think of and have been having no luck. depending on how i change it i get self/player have no attribute strength, maxHp or maxHpCalc is not defined.

class player: def __init__(self, hp = 1 , maxHp = 1, strength = 4, defense = 5): self.hp = hp self.maxHp = maxHpCalc() self.strength = strength self.defense = defense def maxHpCalc(): player.maxHp = player.strength + player.defense class orc(player): def __init__(self, hp, maxHp, strength , defnese): super().__init__(hp, maxHp, strength, defense) print(player.maxHp)

everything i change give me player/self has no attribute strength at the moment


Here's some fixed code, with a small driver program to exercise the classes.

Note the following changes

<ol><li>The function maxHpCalc should be a bound method, if it is to operate on data contained in a player instance. Therefore it should have a self parameter and should reference strength and defense from that self reference.</li> <li>When calling _maxHpCalc you should reference self. I made it a bound method and it needs an instance to work on. I added an underscore to indicate it's a private method.</li> <li>You should call maxHpCalc <strong>after</strong> setting the values of strength and defense, otherwise they are not defined at the point the function is called.</li> <li>player.maxHp makes no sense. player is a class and has no static property maxHp, you need an instance to access that property. I create an instance and reference that.</li> </ol>


class player: def __init__(self, hp=1, maxHp=1, strength=4, defense=5): self.hp = hp self.strength = strength self.defense = defense self.maxHp = self._maxHpCalc() def _maxHpCalc(self): return self.strength + self.defense class orc(player): def __init__(self, hp, maxHp, strength , defense): super().__init__(hp, maxHp, strength, defense) p = player() o = orc(1,2,3,4) print(p.maxHp) print(o.maxHp)

I also have to ask, why include a constructor parameter maxHp if you don't use it but calculate it from other parameters?


  • Select nodes with specific attribute children nodes using xpath
  • Global navigation with active selector (PHP)
  • Function object is not subscriptable in Python?
  • Using AT commands with an Android phone
  • FM signal strength of Android
  • use if with NSinteger?
  • Azure AD B2C password expiration
  • Python for loop slows and evenutally hangs
  • Does c# have a rspec like testing framework that makes grouping like tests together?
  • Change day number color on click - not background
  • Hibernate UserType and a defined length
  • Shut down the underlying Executor of ExecutorCoroutineDispatcher
  • Cannot convert a char value to money. The char value has incorrect syntax
  • Is there a chance to get -splash: work for SWT applications that require -XstartOnFirstThread?
  • Python Tkinter after event OOPS implementation
  • MRO with multiple inheritance in python
  • include dlls in visual studio c++ 2008
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • R Split data.frame using a column that represents and on/off switch
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • error importing numpy
  • Grails calculated field in SQL
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • jQuery ready not fired after rails link_to is clicked
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • Yii2: Config params vs. const/define
  • Algorithm for a smudge tool?
  • HTML download movie download link
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • Return words with double consecutive letters
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Net Present Value in Excel for Grouped Recurring CF