|
| 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* |
0 commit comments