3 minute read

Introduction

Today, I tried to create a rock-paper-scissors game using object-oriented programming in Python — and I ran into more challenges than I expected. Here’s a breakdown of what I struggled with, what questions I asked, and why it was difficult for me as a beginner.


Challenges that I Faced to Understand

1. How to Use __init__ Properly in a Class

I created a Player class with attributes like name, hand, and winrate. I used an __init__ method to initialize them, but I wasn’t sure exactly why I needed it or how it worked behind the scenes.

Why it was hard: I didn’t fully understand what self was doing, and why every class seemed to need __init__. Everything felt very abstract at this stage.

2. Getting Valid User Input

I wanted the player to only input 1, 2, or 3 for their hand (representing scissors, rock, and paper). I tried to use a while loop with input(), and check isdigit(), but I mistakenly wrapped it in int(input()) first — which caused an error when a non-number was entered.

Why it was hard: I didn’t know which part was actually wrong — was it input(), int(), or isdigit()? The error messages were not beginner-friendly at all.

3. Choosing Between 0 and None

I originally initialized self.hand to 0, but then I was told to use None instead. That confused me — isn’t 0 also a valid value?

Why it was hard: I didn’t realize that 0 could be a meaningful game input, whereas None is better for showing that nothing has been selected yet. It took a while to understand this concept of “placeholder vs real value.”

4. Understanding Inheritance

I created a ComputerPlayer class that inherits from Player, and added a select_hand() method using random.randint(1, 3) to simulate the computer’s move.

Why it was hard: I understood inheritance in theory, but in practice, I wasn’t sure when or how select_hand() should be called, or how the structure was supposed to work.


5. Where Should I Create Player Objects?

I imported the Player class into another file where I wanted to handle the game. But then I got confused: do I create p1 and cpu there? Or should the Game class create them? Should they be passed in as parameters?

Why it was hard: I was stuck between thinking procedurally (just make things and run them) and object-oriented (design classes and relationships). I didn’t know where responsibilities should go.


6. Overall Class Design Was Overwhelming

Since I’m new to object-oriented programming, I kept asking myself, “Am I doing this the right way?” I often felt like I was blindly following examples without understanding the reason behind the structure.

Why it was hard: It wasn’t just writing code anymore — it was about designing relationships between pieces like Player, ComputerPlayer, and Game. That felt like something only professionals would know how to do.


What I learned

Today, I learned how to make a simple rock-paper-scissors game using classes in Python. I found out how the __init__() function sets up things like a player’s name and hand, and why self is needed inside a class. I also learned that None is better than 0 when something hasn’t been chosen yet. For user input, I practiced using a while loop with isdigit() to make sure the player types a number. I tried class inheritance by making a ComputerPlayer from Player and giving it a random hand. Lastly, I learned that just importing a class isn’t enough—you also have to create the objects yourself, and knowing where to do that is important.


What I want to do next

Next, I want to keep practicing so I can get more comfortable with this structure. I plan to do more small exercises using classes and then try building a simple project that uses objects properly. This will help me understand how to use what I learned in real situations.

Tags:

Updated: