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/01-running.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,8 +12,8 @@ def main():
12
12
Below that, you'll see an `if` statement calling that function:
13
13
14
14
```python
15
-
`if__name__=="__main__":
16
-
main()`
15
+
if__name__=="__main__":
16
+
main()
17
17
```
18
18
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.
Copy file name to clipboardExpand all lines: responses/02-multiple.md
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,10 +27,9 @@ We would then have a set of commands it would do for every `i`. We already have
27
27
28
28
## Manipulating Variables
29
29
30
-
With a `for` loop implemented, your code takes the form of this:
30
+
With a `for` loop implemented, your main function takes this form:
31
31
32
32
```python
33
-
import random
34
33
dice_rolls =2
35
34
for i inrange(0,dice_rolls):
36
35
roll = random.randint(1,6)
@@ -57,20 +56,24 @@ dice_sum += roll
57
56
58
57
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
58
```python
60
-
print('You have rolled a total of {dice_sum}')
59
+
print(f'You have rolled a total of {dice_sum}')
61
60
```
62
-
Make sure this statement is not indented (since that would include it in the for loop). Overall, your code should look like this:
61
+
Make sure this statement is not indented (since that would include it in the for loop). Overall, your main function should look like this:
63
62
64
63
```python
65
-
import random
66
64
dice_rolls =2
67
65
dice_sum =0
68
66
for i inrange(0,dice_rolls):
69
67
roll = random.randint(1,6)
70
68
dice_sum += roll
71
69
print(f'You rolled a {roll}')
72
-
print('You have rolled a total of {dice_sum}')
70
+
print(f'You have rolled a total of {dice_sum}')
73
71
```
74
72
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
73
76
-
*[Push your code]({{ repoUrl }}/issues/1) to GitHub to continue*
Copy file name to clipboardExpand all lines: responses/02-variables.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,12 +35,14 @@ To use a package, we'll need to import it. At the top of the file, import the pa
35
35
import random
36
36
```
37
37
38
-
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:
38
+
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.
39
+
40
+
To get a random integer with the `random` package, we'll update the `roll` line like so:
39
41
40
42
```python
41
-
random.randint(X,Y)
43
+
roll =random.randint(1,6)
42
44
```
43
45
44
-
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.
46
+
The `random.randint` function takes two integers as parameters. In this case, since we want to emulate a six-sided die, we want to include the range of results that is possible: 1, 2, 3, 4, 5 and 6. Note that the beginning and the end of the range are inclusive bounds. That is, 1 and 6 are possible values, along with integers between them.
45
47
46
48
Now run the code and put *the number* you rolled as a comment, and then we'll keep this tutorial *rolling*!
Copy file name to clipboardExpand all lines: responses/03-lucky.md
+13-7Lines changed: 13 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,15 @@ We have a working dice roller right now! It's useful for a very specific situati
6
6
7
7
Instead of having `dice_rolls` set to a specific value, let's set it equal to:
8
8
```python
9
-
int(input('How many dice would you like to roll?'))
9
+
int(input('How many dice would you like to roll?'))
10
10
```
11
11
12
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
13
14
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
15
16
16
```python
17
-
dice_size =int(input('How many sides are the dice?'))
17
+
dice_size =int(input('How many sides are the dice?'))
18
18
```
19
19
20
20
Change the max value of our roll like this
@@ -30,12 +30,12 @@ elif roll == dice_size:
30
30
31
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
32
33
-
Your final code should look like this:
33
+
Your final main function should look like this:
34
34
35
35
```python
36
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?'))
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
39
dice_sum =0
40
40
for i inrange(0,dice_rolls):
41
41
roll = random.randint(1,dice_size)
@@ -46,6 +46,12 @@ for i in range(0,dice_rolls):
46
46
print(f'You rolled a {roll}! Critical Success!')
47
47
else:
48
48
print(f'You rolled a {roll}')
49
-
print('You have rolled a total of {dice_sum}')
49
+
print(f'You have rolled a total of {dice_sum}')
50
50
```
51
-
*[Push your code]({{ repoUrl }}/issues/1) to GitHub to finish the course!*
Copy file name to clipboardExpand all lines: responses/03-unlucky.md
+13-8Lines changed: 13 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,15 @@ We have a working dice roller right now! It's useful for a very specific situati
6
6
7
7
Instead of having `dice_rolls` set to a specific value, let's set it equal to:
8
8
```python
9
-
int(input('How many dice would you like to roll?'))
9
+
int(input('How many dice would you like to roll?'))
10
10
```
11
11
12
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
13
14
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
15
16
16
```python
17
-
dice_size =int(input('How many sides are the dice?'))
17
+
dice_size =int(input('How many sides are the dice?'))
18
18
```
19
19
20
20
Change the max value of our roll like this
@@ -30,12 +30,11 @@ elif roll == dice_size:
30
30
31
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
32
33
-
Your final code should look like this:
33
+
Your final main function should look like this:
34
34
35
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?'))
36
+
dice_rolls =int(input('How many dice would you like to roll? '))
37
+
dice_size =int(input('How many sides are the dice? '))
39
38
dice_sum =0
40
39
for i inrange(0,dice_rolls):
41
40
roll = random.randint(1,dice_size)
@@ -46,6 +45,12 @@ for i in range(0,dice_rolls):
46
45
print(f'You rolled a {roll}! Critical Success!')
47
46
else:
48
47
print(f'You rolled a {roll}')
49
-
print('You have rolled a total of {dice_sum}')
48
+
print(f'You have rolled a total of {dice_sum}')
50
49
```
51
-
*[Push your code]({{ repoUrl }}/issues/1) to GitHub to finish the course!*
0 commit comments