Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/04-Milestone Project - 1/00-Warm-Up-Project-Exercises.ipynb
Views: 648
Warm Up Project Exercises
It is time to get you to put together all your skills to start building usable projects! Before you jump into our full milestone project, we will go through some warm-up component exercises, to get you comfortable with a few key ideas we use in the milestone project and larger projects in general, specifically:
Getting User Input
Creating Functions that edit variables based on user input
Generating output
Joining User Inputs and Logic Flow
Function to Display Information
Creating a function that displays a list for the user
Accepting User Input
Creating function that takes in an input from user and returns the result in the correct data type. Be careful when using the input() function, running that cell twice without providing an input value will cause python to get hung up waiting for the initial value on the first run. You will notice an In[*] next to the cell if it gets stuck, in which case, simply restart the kernel and re-run any necessary cells.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-202dd8101f66> in <module>()
1 # Example of an error!
----> 2 result = int(input("Please enter a number: "))
ValueError: invalid literal for int() with base 10: 'two'
** Creating a function to hold this logic: **
Validating User Input
** Check that input is valid before attempting to convert.**
We'll use a simple method here.
As you get more advanced, you can start looking at other ways of doing this (these methods will make more sense later on in the course, so don't worry about them for now).
** Edit the function to confirm against an acceptable value or type **
Let's try adding an error message within the while loop!
Now let's explore how to "clear" the output, that way we don't see the history of the "Choose a number" statements.
**NOTE: Jupyter Notebook users will use the IPython method shown here. Other IDE users (PyCharm, VS, etc..) will use **
Checking Against Multiple Possible Values
More Flexible Example
Simple User Interaction
Finally let's combine all of these ideas to create a small game where a user can choose a "position" in an existing list and replace it with a value of their choice.
Game Logic All Together
Great work! You now have an understanding of bringing functions and loop logics together to build a simple game. This will be expanded upon in the Milestone project!