You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: responses/02-multiple.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ import random
34
34
dice_rolls =2
35
35
for i inrange(0,dice_rolls):
36
36
roll = random.randint(1,6)
37
-
print(f'You rolled a {roll}')`
37
+
print(f'You rolled a {roll}')
38
38
```
39
39
40
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:
@@ -59,6 +59,18 @@ It doesn't matter which version you prefer, just go with whichever way is easier
59
59
```python
60
60
print('You have rolled a total of {dice_sum}')
61
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!
62
+
Make sure this statement is not indented (since that would include it in the for loop). Overall, your code should look like this:
63
63
64
-
*[push your code]({{ repoUrl }}/issues/1) to GitHub to continue*
64
+
```python
65
+
import random
66
+
dice_rolls =2
67
+
dice_sum =0
68
+
for i inrange(0,dice_rolls):
69
+
roll = random.randint(1,6)
70
+
dice_sum += roll
71
+
print(f'You rolled a {roll}')
72
+
print('You have rolled a total of {dice_sum}')
73
+
```
74
+
If you save and run the file, you'll now get the values of the two dice rolls and their combined sum. Now our code is developing some complexity!
75
+
76
+
*[Push your code]({{ repoUrl }}/issues/1) to GitHub to continue*
Copy file name to clipboardExpand all lines: responses/02-variables.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,9 +14,7 @@ We'll want to print that variable, too, so let's change the print function to re
14
14
print(f'You rolled a {roll}')
15
15
```
16
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.
17
+
Run the code again, and it should say `You rolled a 5`. Nice! Now we're getting a number. A die that only rolls 5's isn't useful though, so let's add in the randomness that dice are used for.
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 inrange(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
1
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
2
17
-
In Python, we can set this as the criteria by replacing our print statement with:
3
+
In Python, we can set this as the criteria by replacing our print statement in the loop with:
18
4
19
5
```python
20
6
if roll ==1:
@@ -37,4 +23,22 @@ elif roll == 6:
37
23
print(f'You rolled a {roll}! Critical Success!')
38
24
```
39
25
26
+
With the conditionals added, your code should look something like this:
27
+
28
+
```python
29
+
import random
30
+
dice_rolls =2
31
+
dice_sum =0
32
+
for i inrange(0,dice_rolls):
33
+
roll = random.randint(1,6)
34
+
dice_sum += roll
35
+
if roll ==1:
36
+
print(f'You rolled a {roll}! Critical Fail')
37
+
elif roll ==6:
38
+
print(f'You rolled a {roll}! Critical Success!')
39
+
else:
40
+
print(f'You rolled a {roll}')
41
+
print(f'You have rolled a total of {dice_sum}')
42
+
```
43
+
40
44
Leave a comment with the *number of rolls* it took to get a Critical Success.
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 inrange(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
1
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
2
23
3
Instead of having `dice_rolls` set to a specific value, let's set it equal to:
@@ -33,12 +13,35 @@ Tabletop games use dice with all number of sides so it'll be useful to add a use
33
13
dice_size =int(input('How many sides are the dice?'))
34
14
```
35
15
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:
16
+
Change the max value of our roll like this
17
+
```python
18
+
roll = random.randint(1,dice_size)
19
+
```
20
+
21
+
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
22
38
23
```python
39
24
elif roll == dice_size:
40
25
```
41
26
42
27
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
28
44
-
*[push your code]({{ repoUrl }}/issues/1) to GitHub to finish the course!*
29
+
Your final code should look like this:
30
+
31
+
```python
32
+
import random
33
+
dice_rolls =int(input('How many dice would you like to roll?'))
34
+
dice_size =int(input('How many sides are the dice?'))
35
+
dice_sum =0
36
+
for i inrange(0,dice_rolls):
37
+
roll = random.randint(1,dice_size)
38
+
dice_sum += roll
39
+
if roll ==1:
40
+
print(f'You rolled a {roll}! Critical Fail')
41
+
elif roll == dice_size:
42
+
print(f'You rolled a {roll}! Critical Success!')
43
+
else:
44
+
print(f'You rolled a {roll}')
45
+
print('You have rolled a total of {dice_sum}')
46
+
```
47
+
*[Push your code]({{ repoUrl }}/issues/1) to GitHub to finish the course!*
Wow, it took {{ rollNum }} rolls? 😮 That is {{ rollNum/6 }} longer than it should have taken! Oh well, you don't need good luck if you have crazy python skills! 😎 🐍
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 inrange(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!*
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! 😎 🐍
0 commit comments