Skip to content

Commit 90265f2

Browse files
committed
fix final step
1 parent 3927b1d commit 90265f2

File tree

4 files changed

+104
-58
lines changed

4 files changed

+104
-58
lines changed

config.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,13 @@ steps:
7878
with: 03-unlucky.md
7979
data:
8080
rollNum: '%payload.comment.body%'
81-
- type: respond
82-
with: 03-input.md
8381
- type: respond
8482
with: 03-lucky.md
8583
data:
8684
rollNum: '%payload.comment.body%'
87-
- type: respond
88-
with: 03-input.md
89-
issue: 3
90-
data:
91-
rollNum: '%payload.comment.body%'
9285

93-
- title: Rolling multiple Dice
94-
description: Roll multiple dice and add their results
86+
- title: Adding User Input
87+
description: Allow users to input the number and size of dice
9588
event: push
9689
link: "{{ repoUrl }}/issues/3"
9790
actions:

responses/03-input.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

responses/03-lucky.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
Wow, it only took {{ rollNum }} rolls? 🤩 You must be lucky! 🍀
1+
Wow, it only took {{ rollNum }} rolls? 🤩 You must be lucky! 🍀
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!*

responses/03-unlucky.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
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

Comments
 (0)