Loading video player…

Building the Text-Based User Interface

00:00 Open up your IDE or text editor of choice and start by creating a fresh, empty Python file. The approach you’ll take for each step of building the app is going to be to start by writing comments or docstrings to describe what the following code or function will be doing, and then write the code.

00:15 We have code for each lesson provided in the downloadable course resources, and it’ll make things a lot easier if you have that available as a reference while building your app. Define the app’s main code block with a comment, and the first task will be to get and validate user’s input.

00:32 First, you define the variable num_dice_input, and in it store the return value of the input() function with the string argument “How many dice do you want to roll? [1 to 6]” The way the input() function works when this line of code runs, the string that you pass becomes a prompt to the user, and their response via the command line, will be the return value of the function as a string.

00:52 Next, the variable num_dice is the output of the parse_input() function that you have yet to write accepting the argument, num_dice_input.

01:00 Now you have to define the parse_input() function, and you might wonder, well, why do I need the parse_input() function? First, the input() function always returns a string, so that will need to be converted to an integer, and you want to be sure num_dice ends up containing a valid integer one through six, because odds are the user could try passing an invalid input and to avoid taking any chances, you’ll need to verify the input before program execution continues.

01:28 Place this function above the app’s main code block: def parse_input(input_string):

01:36 and then add a docstring describing the function’s return value and behavior. “Return input string as an integer between 1 and 6.”

01:46 Check if input_string is an integer number between 1 and 6.

01:53 If so, return an integer with the same value.

01:59 Otherwise, tell the user to enter a valid number and quit the program.

02:04 If you want an extra challenge, you can pause here and write the code that does this yourself. If not, follow along as we implement the functionality. `if input_string.strip() in the set of strings {“1”, “2”, “3”, “4”, “5”, “6”}:`

02:23 return int(input_string.strip()) This block of code first uses the string method .strip() to strip the user’s input of any extra white space.

02:33 Then checks if they’ve input a valid number. If so, it’ll return that number as an int, else use the print() function to print a message to your user: “Please enter a number from 1 to 6,”

02:47 then raise the exception SystemExit with the argument 1. This ends the program reporting the exit code 1, which is a standard exit code used when a program terminates with an error.

02:59 And save. Now you’re ready to run the code you’ve written so far and see if everything is working as expected. I’ll use the integrated terminal for this: python dice.py, and here’s the prompt: “How many dice do you want to roll? [1 to 6]” We’ll try four, and nothing happens, which is exactly what we expect because we haven’t written any code yet to handle the user’s input.

03:22 After validation, try running the code again and giving it an invalid input like nine. Perfect. The message you wrote appears and nothing else happens. Okay, you’re capturing and validating user inputs. What now?

03:37 Well, in the next lesson, you’ll raise the stakes by implementing the simulated random dice rolls.

Become a Member to join the conversation.