|
1 | | -Wow, it took {{ rollNum }} rolls? 😮 That is {{ rollNum/3 }} times longer than it should have taken! Oh well, you don't need good luck if you have crazy python skills! 😎 🐍 |
| 1 | +Wow, it took {{ rollNum }} rolls? 😮 That is {{ rollNum/3 }} times longer than it should have taken! Oh well, you don't need good luck if you have crazy python skills! 😎 🐍 |
| 2 | + |
| 3 | +## Adding User Input |
| 4 | + |
| 5 | +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`. |
| 6 | + |
| 7 | +Instead of having `dice_rolls` set to a specific value, let's set it equal to: |
| 8 | +```python |
| 9 | +int(input('How many dice would you like to roll?')) |
| 10 | +``` |
| 11 | + |
| 12 | +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! |
| 13 | + |
| 14 | +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: |
| 15 | + |
| 16 | +```python |
| 17 | +dice_size = int(input('How many sides are the dice?')) |
| 18 | +``` |
| 19 | + |
| 20 | +Change the max value of our roll like this |
| 21 | +```python |
| 22 | +roll = random.randint(1,dice_size) |
| 23 | +``` |
| 24 | + |
| 25 | +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: |
| 26 | + |
| 27 | +```python |
| 28 | +elif roll == dice_size: |
| 29 | +``` |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +Your final code should look like this: |
| 34 | + |
| 35 | +```python |
| 36 | +import random |
| 37 | +dice_rolls = int(input('How many dice would you like to roll?')) |
| 38 | +dice_size = int(input('How many sides are the dice?')) |
| 39 | +dice_sum = 0 |
| 40 | +for i in range(0,dice_rolls): |
| 41 | + roll = random.randint(1,dice_size) |
| 42 | + dice_sum += roll |
| 43 | + if roll == 1: |
| 44 | + print(f'You rolled a {roll}! Critical Fail') |
| 45 | + elif roll == dice_size: |
| 46 | + print(f'You rolled a {roll}! Critical Success!') |
| 47 | + else: |
| 48 | + print(f'You rolled a {roll}') |
| 49 | +print('You have rolled a total of {dice_sum}') |
| 50 | +``` |
| 51 | +*[Push your code]({{ repoUrl }}/issues/1) to GitHub to finish the course!* |
0 commit comments