I don’t know exactly how to explain it, but I’ll try my best. Here is a simple OOP example where you create players to kill a dragon. The point is to assign to each player a specific amount of damage to inflict on a dragon, in order to kill it. For instance, if a dragon has 200 health and 2
Tag: oop
How can I call a class method on an instance within a different class __init__
I am creating a tic-tac-toe game in python. I’m trying to call the update method from my Board class on my boardState object within the class init of my Turn class. When I run it I get NameError: name boardState is not defined. Answer you are getting this error siense you haven’t defined “boardState” before referencing it, you need to
Time limit exceeded when finding tree height
this is my code to find the height of a tree of up to 10^5 nodes. May I know why I get the following error? Warning, long feedback: only the beginning and the end of the feedback message is shown, and the middle was replaced by ” … “. Failed case #18/24: time limit exceeded Input: 100000 Your output: stderr:
How to use dictionary in oop to store data
i want to ask you i want to store all informations in dictionary for this code but i can’t i know that something i did wrong.I like to store informations from Customers if issue take a hourly , monthly or daily it is on BikeRental request bikes what i tried by far is any way? Answer If you just wanted
Tkinter – How to create a custom Tkinter Frame object? I tried but it doesn’t work
I tried to create some custom tkinter objects to build my GUIs easier but unfortunately I couldn’t. For example, what about if I want an object that identify a frame placed inside another one? To do that I wrote it: Main code (file name: test.py): Library (file name: ttk_plus.py): If I run test.py, the software start without any kind of
Simple question on how to create subclasses?
I’m new to the idea of object-oriented programming in Python, and the school has given me a question to first create a class called “Mammal”, with an attribute “name” and method get_name(). Hence I came up with this: Seems alright. The next part of the question asks me to create a subclass Dog which has the name dog and the
Cleaner Alternative to Nested If/Else
I’m mainly focused on an alternative to if/else’s in create_animal. If there is a more professional way to handle this. In this case, it’s a classifier based on a variable number of traits needed to figure out what the animal is. Since humans are the only animals that speak English, that property is sufficient. But if they roar instead Bear
How to convert Python Integer to Roman Numeral and vice-versa? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 12 months ago. Improve this question I am trying to write a Python program that accepts integer as input and displays the equivalent roman numeral and
c++ equivalent to python self.attribute = ObjectInstance()
i want to know if there is an equivalent way of doing this in c++: Answer self.b in C++ could be this->b, but also just b as this is implicit in C++. However, in C++, you have to declare (member) variables, while in Python you create them by assigning to them and the type of the variable is determinated by
Short way to get all field names of a pydantic class
Minimal example of the class: The way it works: Is there a way to make it work without giving the class again? Desired way to get all field names: It would also work if the field names are assigned to an attribute. Just any way to make it make it more readable Answer What about just using __fields__: Output: This