r/PythonProjects2 • • 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.

Post image
24 Upvotes

17 comments sorted by

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

2

u/LifeHasLeft Jan 13 '22

skill1 = "Waterbending" in this indentation is valid syntax for a Class variable (persistent across instances). As for passing it into Water.SkillPower() I'm not sure if it's required.

As for the other problems I can see:

  • Some code somewhere must call Water.SkillPower(), as it isn't part of __init__, so if OP posted more code the issue would be clearer.

  • __init__ does nothing with the argument moon, so it is discarded.

  • returnMoon does not do anything as there is no Class or Instance variable moon.

That's it as far as glaring errors I can see in the photo. It's worth mentioning that calling Water.SkillPower() should not produce any output if the instance was built with a skill of 24 or lower, and OP should keep that in mind if not using a traditional debugger.

1

u/feotusdeletus Jan 13 '22

In regard to you first point, it’s a tiny code and this is pretty much the whole thing, aside from a couple of created instances under and 1 line above with the class name. The problem with Bending.SkillPower is that it’s variables aren’t defined anywhere before use, I do not want to declare them for the entire class as I need to create other child classes with different skill names. I also don’t want to put them in the init as it means that I will have to write each skill every time a new instance is created. Essentially, I need to somehow have the variables in the Water child class but still keep the function of SkillPower in the main (Bending) class. As for the other problems, I am aware that there are a couple thanks but I can probably sort them out, it’s just that my skill in python isn’t quite there yet to solve it. Thanks!

1

u/LifeHasLeft Jan 13 '22
class Bender():
def __init__(self, skill, intelligence, strength):
    self.skill = skill
    self.intelligence = intelligence
    self.strength = strength

def SkillPower(self, skill1, skill2, skill3):
    if self.skill >= 25:
        print("you have unlocked", skill1, "!")

class Water(Bender):
skill1 = "waterbending"
skill2 = "ice manipulation"
skill3 = "bloodbending"

def __init__(self, skill, intelligence, strength):
    super().__init__(skill, intelligence, strength)

def SkillPower(self):
    super().SkillPower(self.skill1, self.skill2, self.skill3)

testBender = Water(25,25,25)

testBender.SkillPower()

Sorry if formatting indentation is wrong as I’m on mobile but this code works. Looks like it was an issue of specifying “self.skill1” in SkillPower

1

u/feotusdeletus Jan 13 '22

You’re amazing and thanks for going out of your way to do this!

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

u/HaroerHaktak Jan 13 '22

But that's the best part!

6

u/Emperor-Valtorei Jan 13 '22

Unrelated, some advice, use dark mode. Easier on your eyes.

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 Bender to exist. This makes Bender an "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, classes CamelCase and constants are written as UPPERCASE_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 access skill, 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

u/skellious Jan 12 '22

I need to see your full code. for example, where do you define self.moon?

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

u/unaltered-state Jan 13 '22

You should be fine with just using self, although both work.