JavaScript is My Bread and Butter, But Python is My Future

Sep 4, 2023

Learn Python as a JavaScript developer
As a longtime JavaScript developer, I decided to dip my toes into the Python programming language. What drew me to Python? For starters, AI, and Machine Learning. It's also known for being super simple and flexible—perfect for a beginner like me. In this article, I'll walk you through the process of building a very beginner friendly "Rock, Paper, Scissors" game using Python.

Getting Started

If you’d like to follow along, the first step is to get Python installed on your computer. Fortunately, Python is incredibly accessible and is available for multiple platforms. You can download the latest version of Python from the official Python website Python.org and follow the installation instructions for your specific operating system.

Once Python is up and running, you’ll need a code editor to write and run your Python programs. One of the popular choices among developers (and the one I use) is Visual Studio Code (VSCode), a free and open-source code editor. It offers a seamless Python development experience with handy extensions and a user-friendly interface. You can download VSCode from code.visualstudio.com and add the Python extension to supercharge your Python coding journey.

A great video resource for getting Python and VSCode setup is from Dave Gray. This video is part of a Python series, but I clipped it so you can jump right to “downloading Python”, and “download and install vscode of Python

 

Importing the Random Module

To get things rolling, we’ll need to import the random module, which will allow us to introduce an element of randomness into the game. Python’s random module makes it super easy to generate random choices for the computer.

This line of code imports the `random` module in Python, which allows us to generate random numbers, choices, and other random operations.

 

Defining the Game Rules and Options

Here, we define the game rules and options for “rock, paper, scissors.” The `rules` dictionary maps each option (rock, paper, or scissors) to what it can defeat. For example, “rock” defeats “scissors,” “paper” defeats “rock,” and “scissors” defeats “paper.” The `options` list contains the available choices for the player and the computer.

 

Game Loop

The heart of our game lies in the `while True:` statement that creates an infinite loop. The game will continue until the player decides to stop playing.

 

Getting User Input

This line prompts the user to enter their choice (rock, paper, or scissors). The `input()` function takes the user’s input as a string and stores it in the `user_choice` variable.

 

Generating Computer’s Choice

The computer’s choice is randomly generated using `random.choice()`. It selects a random item from the `options` list (rock, paper, or scissors) and stores it in the `computer_choice` variable.

 

Displaying Choices

This line prints the user’s choice and the computer’s choice using an f-string (formatted string). It displays the choices in the output for the user to see.

 

Determining the Winner

In this part, we check for the winner or if it’s a tie. If the user’s choice is the same as the computer’s choice, it’s a tie. If the user’s choice defeats the computer’s choice according to the game rules defined earlier, the user wins. Otherwise, the computer wins.

 

Asking to Play Again

After each round, the user is asked if they want to play again. The input is stored in the `play_again` variable. If the user’s input is not “y” (meaning they don’t want to play again), the loop is exited using `break`.

 

Exiting the Game

After the user decides not to play again, the loop is exited, and the program prints a “Thanks for playing!” message.

 

Running the Program

This comment indicates how to run the Python script (assuming the script file is named “rock-paper-scissors-game.py”). You would run this in the terminal or command prompt, and the game would start, allowing you to play against the computer in a “rock, paper, scissors” match.

 

Conclusion

And that’s it, a simple Python program: the “rock paper scissors” game.

In a nutshell, Python is incredibly friendly as a JavaScript developer. Its straightforward syntax and clear structure make it easy to grasp, even for those new to the language.

You won’t find yourself battling complex syntax. Instead, you can focus on the real essence of programming: logic, data manipulation, and algorithms. Python is the perfect first programming language for someone looking to learn to code. It’s also a fantastic option for JavaScript developers who want to expand their skill set without the headache of a steep learning curve.

Happy coding!

Recent posts

How to Create Impactful Marketing Messages

How to Create Impactful Marketing Messages

Click to jump ahead Mission Statement Customer Pain-points Value Proposition USP (Unique Selling Proposition) Marketing Angle What’s a Marketing Message?   Let’s start with the obvious. What is a marketing message? A marketing message is your elevator pitch -...

Web Scraping: How to Extract Data From Websites

Web Scraping: How to Extract Data From Websites

With web scraping, you can inspect web pages, gather specific data points, and then use this information for analysis, research, or any other purpose you desire. With just a few lines of code, you'll be well on your way to becoming a serious web scraping dev. If you...

Get in touch with me.

I love connecting with other builders, creators, and marketing nerds. Let’s chat.

If you like this type of content, you should consider subscribing to my newsletter.

About once a month I do an information dump where I share all of the digital marketing insights I’ve uncovered helping my clients grow their businesses online. If you’re a business nerd like me, you’ll love the insights I share.

    No spam, ever. Unsubscribe anytime.