Skip to content

Commit 0cd5c9f

Browse files
committed
add initial course materials
0 parents  commit 0cd5c9f

File tree

11 files changed

+410
-0
lines changed

11 files changed

+410
-0
lines changed

config.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
title: Intermediate Python
2+
tagline: Create a REST api with Node.js, Express.js, and MongoDB
3+
description: Learn how to create a dice-rolling app with Python!
4+
tags:
5+
- Python
6+
7+
template:
8+
name: intermediate-python-course
9+
repo: intermediate-python-course-template
10+
description: an intermediate Python course
11+
before:
12+
- type: createIssue
13+
title: Set up your project
14+
body: 01-setup.md
15+
16+
steps:
17+
- title: Set up your project
18+
description: Introduces the project and how to set it up
19+
event: issue_comment
20+
link: "{{ repoUrl }}/issues/1"
21+
actions:
22+
- type: respond
23+
with: 01-running.md
24+
issue: 1
25+
26+
- title: Running a Python program
27+
description: Run a python program with a main method
28+
event: push
29+
link: "{{ repoUrl }}/issues/1"
30+
actions:
31+
- type: respond
32+
with: 01-complete.md
33+
issue: 1
34+
- type: closeIssue
35+
issue: 1
36+
- type: createIssue
37+
title: Using Variables
38+
body: 02-model.md
39+
40+
- title: Using Variables
41+
description: Define variables with random input to simulate a die-roll
42+
event: issue_comment
43+
link: "{{ repoUrl }}/issues/2"
44+
actions:
45+
- type: respond
46+
with: 02-multiple.md
47+
issue: 2
48+
data:
49+
roll: '%payload.comment.body%'
50+
51+
- title: Rolling multiple Dice
52+
description: Roll multiple dice and add their results
53+
event: push
54+
link: "{{ repoUrl }}/issues/2"
55+
actions:
56+
- type: respond
57+
with: 02-complete.md
58+
issue: 2
59+
- type: closeIssue
60+
issue: 2
61+
- type: createIssue
62+
title: Adding Conditionals
63+
body: 03-conditionals.md
64+
65+
- title: Adding Conditionals
66+
description: Add some flavor to your responses with conditionals
67+
event: issue_comment
68+
link: "{{ repoUrl }}/issues/3"
69+
actions:
70+
- type: gate
71+
left: '%payload.comment.body%'
72+
operator: <
73+
right: 6
74+
- else: respond
75+
with: 03-unlucky.md
76+
data:
77+
rollNum: '%payload.comment.body%'
78+
- type: respond
79+
with: 03-input.md
80+
issue: 3
81+
data:
82+
rollNum: '%payload.comment.body%'
83+
84+
- title: Rolling multiple Dice
85+
description: Roll multiple dice and add their results
86+
event: push
87+
link: "{{ repoUrl }}/issues/3"
88+
actions:
89+
- type: respond
90+
with: 03-complete.md
91+
issue: 3
92+
- type: closeIssue
93+
issue: 3
94+

responses/01-complete.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Great!
2+
3+
You will be pushing code to your [own repository]({{ repoUrl }}) several times as a reference.
4+
5+
[Click here to for the next steps]({{ repoUrl }}/issues/2)

responses/01-running.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Running a Python file
2+
3+
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:
4+
5+
```python
6+
def main():
7+
#print('You rolled a die')
8+
```
9+
10+
Below that, you'll see an `if` statement calling that function:
11+
12+
```python
13+
`if __name__== "__main__":
14+
main()`
15+
```
16+
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.
17+
18+
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.
19+
20+
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.
21+
22+
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.
23+
24+
25+
## Pushing your changes
26+
27+
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.
28+
29+
Let's do those three steps:
30+
31+
1. Git Add: `git add dice_roller.py`
32+
2. Git Commit: `git commit -m "I uncommented line 2"`
33+
3. Git Push: `git push`

responses/01-setup.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
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.
2+
3+
## Having Python
4+
5+
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:
6+
7+
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.
8+
9+
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](https://www.python.org/downloads/) website to download it.
10+
11+
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](https://www.python.org/downloads/) website to download it.
12+
13+
## Having Git
14+
15+
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](https://git-scm.com/downloads) website to download it.
16+
17+
## Cloning this Repository
18+
19+
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 {{ repoUrl }}`
20+
21+
Inside the repo you'll see two files:
22+
23+
- `README.md`: a markdown file that details some info about the project
24+
- `dice-roller.py`: a Python file containing the code you'll be building off of
25+
26+
Now that we have everything we need, we can actually begin writing our dice roller! Let's begin!

responses/02-complete.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
🎲🎲 Snake Eyes! 🐍
2+
3+
[Click here]({{ repoUrl }}/issues/3) to learn how to add some custom responses!

responses/02-multiple.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
You rolled a {{ roll }}? 😄 Nice!
2+
3+
## Making Python Roll Multiple Dice
4+
Before moving on, make sure the `main` function contains:
5+
6+
```python
7+
import random
8+
roll = random.randint(1, 6)
9+
print(f'You rolled a {roll}')
10+
```
11+
12+
Now, what if we wanted to roll two dice? Well, one way to do that is to run the file twice. Another way is to duplicate the print statement within the file. However, this would get tedious for larger projects, so let's make use of a great Python feature: `for` loops. A `for` loop iterates over a range of values and performs a set of commands for all of those values.
13+
14+
How many dice do we want to roll? Let's go with two for now. Make a variable called `dice_rolls` below `import random` but above everything else, and set it equal to two.
15+
16+
Now let's make the `for loop`. Right below where you set `dice_rolls` type:
17+
18+
```python
19+
for i in range(0,dice_rolls):
20+
```
21+
22+
Let's unpack what this means. If we were to make this a grammatical sentence it would read something like this:
23+
24+
"`for` every `i`ndex `in` a `range` starting after `0` until the value is equal to `dice_rolls`, do this`:`"
25+
26+
We would then have a set of commands it would do for every `i`. We already have the commands we want to be repeated, we just have to tell Python that by indenting them. Once they're indented, save the file and run it to get the two rolls.
27+
28+
## Manipulating Variables
29+
30+
With a `for` loop implemented, your code takes the form of this:
31+
32+
```python
33+
import random
34+
dice_rolls = 2
35+
for i in range(0,dice_rolls):
36+
roll = random.randint(1,6)
37+
print(f'You rolled a {roll}')`
38+
```
39+
40+
In most game situations, rolling multiple dice is followed by adding their values together. Let's do that now. In Python, arithmetic can be performed on variables just by using the equivalent mathematical symbols. Want to add two variables together? It's as simple as: `variable_sum = variable_1 + variable_2`. Since our dice values are stored in a single updating variable instead of separate variables, however, we'll have to add the values upon themselves. First, make a variable called dice_sum above the `for` loop, and set it equal to 0:
41+
42+
```python
43+
dice_sum = 0
44+
```
45+
46+
We'll be adding each value of `dice_roll` we get to this `dice_sum` variable as we loop through each dice roll. In the `for` loop, right after the `roll` variable is assigned, type:
47+
48+
```python
49+
dice_sum = dice_sum + roll
50+
```
51+
52+
Another way we can do this in Python is using the `+=` operator:
53+
54+
```python
55+
dice_sum += roll
56+
```
57+
58+
It doesn't matter which version you prefer, just go with whichever way is easier for you to visualize. Finally, let's print out our final value for `dice_sum`. After the `for` loop is finished, type:
59+
```python
60+
print('You have rolled a total of {dice_sum}')
61+
```
62+
If you save and run the file, you'll now get the values of the two dice rolls and their combined sum. Neat!
63+
64+
*[push your code]({{ repoUrl }}/issues/1) to GitHub to continue*

responses/02-variables.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Printing Variables
2+
3+
All the file is doing right now is printing the sentence "You rolled a die." But the purpose of rolling a die is to come up with a number, so let's do that now.
4+
5+
We can make a new variable named `roll`. Variables are simple to make in Python: set the variable equal to what you want it to be. Above the print function, create and set a variable called `roll` equal to 5 by writing:
6+
7+
```python
8+
roll = 5
9+
```
10+
11+
We'll want to print that variable, too, so let's change the print function to reflect what our `roll` variable is set to. This is done through Python's `f-string` functionality. By putting an `f` right before the quotes of the string you're printing, you can then print variables within the string. Let's replace the word 'die' in the sentence to our new `roll` variable. This changes the line to say:
12+
13+
```python
14+
print(f'You rolled a {roll}')
15+
```
16+
17+
Run the code again, and it should say `You rolled a 5`. Nice! Now we're getting a number. Go ahead and `push` your code with the output as the comment within `commit` to move on.
18+
19+
A die that only rolls 5's isn't useful though, so let's add in the randomness that dice are used for.
20+
21+
22+
## Randomizing Variables
23+
24+
25+
Just like a real die, we want the number we roll to be random. We'll need to use a package to do so. Python is full of packages; think of them like tools you can use for specific situations. Just like how you need a shovel to dig a hole, you need the `random` package to get a random value in Python.
26+
27+
Right now, you should have a `main` function that contains this:
28+
29+
```python
30+
roll = 5
31+
print(f'You rolled a {roll}')
32+
```
33+
34+
To use a package, we'll need to import it. At the top of the file, import the package `random` by writing:
35+
36+
```python
37+
import random
38+
```
39+
40+
In general, it's good practice to import all packages at the beginning of the script. Now let's use that package to randomize our `roll` variable. To get a random integer with the `random` package, we'll want to use the following format:
41+
42+
```python
43+
random.randint(X,Y)
44+
```
45+
46+
where X and Y are the boundaries of the range you want your random integers to be in. Note that X, the beginning of the range and Y, and the end of the range, are inclusive bounds. Note that X (the beginning of the range) and Y (the end of the range), are inclusive bounds. This means the integers you put as X and Y will be part of possible values. Knowing that, try changing `roll` to equal any integer that is possible to roll on a six-sided die: 1, 2, 3, 4, 5 and 6. There's no need to change anything in the print statement, as we've already tied it to whatever our `roll` variable is.
47+
48+
Now run the code and put *the number* you rolled as a comment, and then we'll keep this tutorial *rolling*!

responses/03-complete.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Good work finishing the intermediate python course!
2+
3+
Now you have an awesome tool to use at your next game night!
4+
5+
Here are some potential *Next Steps* for you:
6+
7+
1. Add more inputs (like player or team name).
8+
2. Store each player's roll totals in separate arrays.
9+
3. Choose a dice-based game that you can fully simulate using python.

responses/03-conditionals.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Adding Conditionals
2+
Now our code is developing some complexity! By adding our variable manipulation into our `for` loop, the code now looks like this:
3+
4+
```python
5+
import random
6+
dice_rolls = 2
7+
dice_sum = 0
8+
for i in range(0,dice_rolls):
9+
roll = random.randint(1,6)
10+
dice_sum += roll
11+
print(f'You rolled a {roll}')
12+
print('You have rolled a total of {dice_sum}')`
13+
```
14+
15+
Let's add some flavor to our responses, dependent on how we roll. We can do this by using conditionals. We'll put a command that only triggers if a certain condition is met. For example, let's add "Critical Fail" in the printed statement if the die roll is a one.
16+
17+
In Python, we can set this as the criteria by replacing our print statement with:
18+
19+
```python
20+
if roll == 1:
21+
print(f'You rolled a {roll}! Critical Fail')
22+
```
23+
24+
But what if it's *not* a one? We need a catch-all for all the other conditions it could be. This is done in Python with the `else` command. Below our `if` statement and the related print function, add an `else` statement:
25+
26+
```python
27+
else:
28+
print(f'You rolled a {roll}')
29+
```
30+
31+
Now we have two different statements that can be printed. Try running your code until you have a roll that is a one and a roll that is something else.
32+
33+
If we have additional conditions we want to add, we can use a third type of conditional in Python: `elif`. This is used for a specific condition after `if` has been used. Try adding this `elif` statement between the `if` and `else` statements:
34+
35+
```python
36+
elif roll == 6:
37+
print(f'You rolled a {roll}! Critical Success!')
38+
```
39+
40+
Leave a comment with the *number of rolls* it took to get a Critical Success.

responses/03-input.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Wow, it only took {{ rollNum }} rolls? 🤩 You must be lucky! 🍀
2+
3+
With the conditionals added, your code should look something like this:
4+
5+
```python
6+
import random
7+
dice_rolls = 2
8+
dice_sum = 0
9+
for i in range(0,dice_rolls):
10+
roll = random.randint(1,6)
11+
dice_sum += roll
12+
if roll == 1:
13+
print(f'You rolled a {roll}! Critical Fail')
14+
elif roll == 6:
15+
print(f'You rolled a {roll}! Critical Success!')
16+
else:
17+
print(f'You rolled a {roll}')
18+
print(f'You have rolled a total of {dice_sum}')
19+
```
20+
21+
We have a working dice roller right now! It's useful for a very specific situation: rolling two six-sided dice. What if we want to change the number of dice we roll? We can manually edit the code each time, but there's a better way to do this: adding a user `input`.
22+
23+
Instead of having `dice_rolls` set to a specific value, let's set it equal to:
24+
```python
25+
int(input('How many dice would you like to roll?'))
26+
```
27+
28+
Running the code will give a prompt with that question and we can input our desired number of dice. One thing to note: the value a user inputs defaults to a `str` type and we need it to be a whole number, or an `int` in Python; that's why we encompass the `input` command in an `int()`. Try it out!
29+
30+
Tabletop games use dice with all number of sides so it'll be useful to add a user input for that too. Below where you set `dice_rolls` add this line:
31+
32+
```python
33+
dice_size = int(input('How many sides are the dice?'))
34+
```
35+
36+
When changing a value we had assumed to be a specific number previously, we need to make sure the rest of our code still makes sense. All dice start with one, so our `if` statement regarding one still makes sense. Our `elif` statement regarding six doesn't though, as it is no longer guaranteed to be the highest number. Let's make it so our "Critical Success" line is printed to whatever the highest number of our desired dice is. The highest number a dice can roll is the same as the number of sides, so we just to replace our `elif` statement with:
37+
38+
```python
39+
elif roll == dice_size:
40+
```
41+
42+
Try it out! Now you can manually input the number of dice and number of sides on the dice knowing that the `Critical Success!` line will realign accordingly.
43+
44+
*[push your code]({{ repoUrl }}/issues/1) to GitHub to finish the course!*

0 commit comments

Comments
 (0)