Finishing the App and Rolling the Dice
00:00
Okay, it’s time to bring it all together. Go back to the app’s main code block, and let’s review what we’ve got so far. First, you get the user’s input with a prompt from the input()
function followed by parsing and validating it with parse_input()
, which you defined.
00:14
Then you generate the dice rolls with some help from the random
module using the roll_
dice()
function that you wrote.
00:20 The next step is to generate the ASCII diagram of the dice faces.
00:28
Define dice_face_diagram
as the output of calling, generate_dice_faces_diagram()
and passing in the argument roll_results
.
00:37
And finally, display the diagram. You can display the diagram to the user by passing dice_face_diagram
to the built-in print()
function using an f-string here, you print a new line before interpolating dice_face_diagram
.
00:51 That extra line will separate the prompt from the output and make things a little bit cleaner. Alright, it’s time to save and try it out. Using the integrated terminal and creating a little more space here to display this,
01:04
python dice.py
. Okay, it’s the moment of truth. You might be tempted to blow on your keyboard for luck at this point, but depending on how often you dust, that might not be a good idea.
01:16 Okay, how many dice do you want to roll? Five. Alright, it worked. The die is cast or the dice are cast, I guess. Now the app is pretty much done, but nothing’s ever really done, is it?
01:31
Let’s look back at the generate_dice_faces_diagram()
function.
01:37 There’s a lot going on here, isn’t there? In programming, we have a concept known as the single responsibility principle. Basically, a given function, class, or module should only be responsible for one thing.
01:48 This principle makes your code easier to understand and easier to maintain. Functions are more readable, and the effects of any changes you make to them will be limited in scope, thus simplifying testing and potential debugging.
02:01
In the next lesson, you’ll refactor this generate_dice_faces_
diagram()
using a technique known as the extract method.
Become a Member to join the conversation.