Category: Blog
-
Understanding self in Python Classes: A Deep Dive with Turtle Crossing
Understanding self in Python Classes: A Deep Dive with Turtle Crossing When developing a game or any object-oriented Python program, understanding how to manage class instances is crucial. This is where the concept of self comes into play. Recently, while working on my Turtle Crossing game, I encountered a challenge that highlighted the importance of…
-
Understanding Inheritance and Boilerplate in Python Projects
While writing some lines of code for a project I am working on called PONG, I got confused about when to use the inheritance approach. Initially, I created an object using a class and then recalled it to add attributes or methods. This method seemed to work, but I noticed something interesting when I compared…
-
The Importance of Attribute Order in Class Initialization
When writing code for a game called Snake, I encountered an error while trying to run the program. Here’s the error message: Understanding the Error This error occurs because the create_snake method is attempting to use the self.x attribute before it has been initialized. Let’s examine the problematic code: In the above code, self.create_snake() is…
-
Effortlessly Extract and Print Dictionary Items with Units in Python
Extracting and printing specific items from a dictionary in Python could be a nightmare. You may want to print only the values or keys. Maybe you want to print both in one report or what could you do if you are adding additional information such as units? Let’s say you have a dictionary with a…
-
Efficiently Picking Random Elements in Python: randint vs. choice
If you have a list and you want to randomly choose an element from that list you can use, as I did, a complex or easy way. Maybe you have a list with a nested dictionary with a celebrity’s name, number of followers, career, and country of origin. One way to do this is by…
-
Mastering Control Flow: Using While Loops and Conditionals to Meet Specific Conditions
Within the logic of the Blackjack game, there is a condition that must be met. After the user picks his cards, it is time for the computer to play. The computer has to ask for a card when the sum of its deck is less than 16. The original logic was: Input: Output: computer’s final…
-
Efficient List Manipulation in Python: When to Use ‘append’ and ‘extend’
Let’s say I created a function in Python to deal a group of cards. The Ace is represented by 11, Jack, Queen, and King by 10 and the rest of the deck shows its face value. This function will randomly take an element from the list named cards and return a list with a number.…