MaferPs / intermediate-python-course

an intermediate Python course
https://lab.github.com/everydeveloper/intermediate-python
0 stars 0 forks source link

Set up your project #1

Open github-learning-lab[bot] opened 4 years ago

github-learning-lab[bot] commented 4 years ago

Welcome to this (mildly) advanced Python tutorial! Today we'll be writing a script to mimic a common real-world action. If you've ever played a tabletop game, you know there are many dice rolls to make. This tutorial will show how to harness a combination of Python skills to make an automatic dice roller. But first, before we do that, we'll need to get a few things set up on your system.

Having Python

First, in order to do anything in Python, you need to have Python on your computer! Let's make sure it's installed. Open up a terminal and type `python -V. There are a few possible things it can output here:

If the output begins with Python 3, you're good to go! This tutorial was tested on a system running Python 3.7.4, but it should be compatible with any version of Python 3.

If the output begins with Python 2, you have Python, but it's an outdated version. You'll need to download Python 3 to follow this tutorial. Go to the Python website to download it.

If you get a command that reads something similar to command not found, no version of Python is on your system. You'll need to go to the Python website to download it.

Having Git

We also need to make sure Git is installed on your system. Check that by typing git --version in a terminal. If it outputs a git version you're good to go! If not, go to the Git website to download it.

Cloning this Repository

Now that we have Git, we can clone the repository containing the building block of the code you'll be writing. In the terminal type git clone https://github.com/MaferPs/intermediate-python-course

Inside the repo you'll see two files:

Now that we have everything we need, we can actually begin writing our dice roller! Let's begin!

Leave a comment with your favorite dice-rolling game to continue.

MaferPs commented 4 years ago

Dungeons and dragons

github-learning-lab[bot] commented 4 years ago

Dungeons and dragons! Wow that's a classic! You might be able to make a version of Dungeons and dragons when we are done!

Running a Python file

Let's look at our Python file. Inside we see a function main that will contain all of your commands as you create the dice roller:

def main():
    #print('You rolled a die')

Below that, you'll see an if statement calling that function:

if __name__== "__main__":
    main()

By doing this, the function main will run whenever you run the Python script. As you develop the dice roller, be sure to only manipulate the code within the main function; nothing else will need to be changed.

Right now the only line in our main function is a print() function:#print('You rolled a die'). This will print whatever is inside the parenthesis. There's a # in front of it right now, which means it's a comment. Uncomment it by removing the # to let Python read it, and save the file.

You can run the Python script by typing python dice_roller.py in a terminal, provided you're in the correct directory. If not, run the script by typing python /path/to/directory/dice_roller.py, where /path/to/directory is replaced with the path to the file on your own system.

You'll see the following nifty, albeit not very useful line printed out in your terminal: "You've rolled a die". We'll make it more informative in a bit, but first, let's push those changes to GitHub.

Pushing your changes

Whenever you change files in your repository, you'll want to add and commit the file, which allows you to add a message detailing what you changed. Then you can push the updated version, along with your comments, to GitHub.

Let's do those three steps:

  1. Git Add: git add dice_roller.py
  2. Git Commit: git commit -m "I uncommented line 2"
  3. Git Push: git push