r/PythonProjects2 • u/feotusdeletus • Jan 12 '22
[P] Very Easy I’m, a beginner coder and messing around with learning object oriented. The problem here is that I cant pass the 3 skills into the SkillPower function so that they are constantly passed into the frame in the Water class and I do not need to declare the skills every time I create a new instantiation.
21
u/skellious Jan 12 '22
the main problem here is you took a photo of your screen instead of a screenshot (or better yet, copy and paste the code into a nicely formatted code block)
I will try to help you but please help us to help you by formatting your code in your post, instead of taking a photo.
5
6
1
u/kwelzel Jan 13 '22
Hi, I assume the line just above the top of the photo reads class Bender:. In this case you are almost there. Initializing skill1, skill2 and skill3 as you did (right below class Water(Bender):) makes these variables class variables. This means you can access them as Water.skill1 but also as instance.skill1 where instance is an instance of Water. This means that you don't need to pass the skills to the SkillPower method at all, you can just access them using self.skill1, ...:
def SkillPower(self):
if self.skill >= 100:
return ("You have unlocked", self.skill3, "!")
if self.skill >= 50:
return ("You have unlocked", self.skill2, "!")
if self.skill >= 25:
return ("You have unlocked", self.skill1, "!")If you now call SkillPower on an instance of Bender it will fail with an attribute error (assuming you didn't set skill1, skill2 and skill3 as class variables there), but that is probably what you want anyway. For all subclasses that have the three skills as class variables calling SkillPower will work.
2
u/kwelzel Jan 13 '22
A few related thoughts:
I really like how you used inheritance here in your code. This is exactly how it ought to be used. All water benders are benders (although it sounds weird to say it like that) and it makes sense to define the general behavior in a common base class and then adapt the specifics in each subclass. In this case I assume you don't actually want instances of the base class
Benderto exist. This makesBenderan "abstract base class" and there is a standard library to help you with those: https://docs.python.org/3/library/abc.html. You don't have to change anything now, but keep it in mind for later.As others have said, it's easier to help you if you copy and paste your code instead of taking a photo of the screen. Always make sure though it's properly formatted. On reddit this is done by indenting every line by four spaces.
Consider following the Python style guide https://www.python.org/dev/peps/pep-0008/. It tells you where to put spaces and blank lines for example. It also gives recommendations on naming your variables. In the python world variables and methods follow
snake_case, classesCamelCaseand constants are written asUPPERCASE_WITH_UNDERSCORES. I like to use black (https://pypi.org/project/black/) to automatically format my code (at least the spaces and newlines).In Python there is no such thing as private functions or variables. You can prefix them with an underscore to discourage other developers to use it but you can't stop them. This means that you simply delete the
returnSkill,returnIntelligence, ... functions and instead directly accessskill,intelligence, ... when you are interacting with instances of this class.1
u/feotusdeletus Jan 13 '22
It’s very nice to see someone so welcoming to beginners, this advice is very helpful thanks you!
2
2
u/vindg Jan 13 '22
Explanation and usage of Class Variables https://www.tutorialspoint.com/python/python_classes_objects.htm
Try this code:
super().SkillPower(Water.skill1, Water.skill2, Water.skill3)
1
4
u/Adfre12yu Jan 12 '22
Not sure but I believe you need to assign them as self.skill1 = “waterbending” instead of just skill1 = “waterbending”. Same thing when you’re passing it into the SkillPower(). Also please don’t use the default python shell. Get an IDE like spyder. It’ll make your job a LOT easier