User Input and Conditionals
In the last lesson we learned how to compare values. In this lesson we will expand that knowledge and learn how to influence programs decision to do something depending on conditions. We will also cover how to get input from the user.
Lesson Overview
In this lesson you will learn:
- How to receive an input from user
- How to make program respond to conditions
- How to use
if-elif-elseto branch your program
User Input
Our program would not be very useful if it cannot take data from the user. In Python we do this using input() function. input() takes any string as a user prompt (what will be displayed to the user while waiting for input)
input() always returns a stringinput() function always returnes a string. This is something you need to watch for when you ask the user to enter some data. If you need numbers (integer or float) you will need to type cast it to some other type.
If you cannot remember how to do this, check out previus Core Datatypes - Type Casting lesson.
Conditionals - What Are They?
Conditionals is a name for a group of keywords that branch the program logic depending on condition. In Python we have:
ifelifelse
We use these keywords to make our program do something if one condition is True and something else if its False.
We start by using if keyword, followed by a condition and end it with colon (:).
Then we must indent (4 spaces or 1 tab) the next line.
Let's see a simple example by running the code below. Feel free to change variable a to see how the program reacts.
In this basic example, our program will execute its task depending on conditions. In the current example, we ask Python to compare value in variable a to int 5. If they are equal, program executes one branch and if its not, program executes another branch.
Unlike other programming languages that use curly braces to mark blocks (sections) of code, in Python we use indentation. This is extremely important to learn as Python will raise IndentationError if you do it wrong.
In Python, we also have elif keyword which means else-if. Its used to check more than one condition. Let's see an example:
In this example, we check the value of variable a. Then based on conditions set, our program will execute specific branch.
We use this branching logic to make our program do varius things based on these conditions. Each condition we have always evaluates to boolean - being True or False. We can also use logical operators (and, or, not) to chain multiple conditions for Python to check.
In below assigment you are tasked with checking some rules and exiting application if any are broken.
You can use built-in module called sys which has some handy functionality to work with our operating system. For now, we are only interested in using its exit() function. It takes in int value to provide as exit code which other applications can use to interpret whether your program run successfully or failed and in what way.
It is common to use 0 as success and any other positive integer to signify a failure.
To use other modules, you use import keyword, followed by the name of the module. Usually, these are located at the top of the file, before writing your logic.
You can use it like this:
import sys # once at the top of the file
sys.exit(0) # success
sys.exit(1) # failed / error
We will learn more about built-in, custom and third-party modules (called libraries) later in the course.
Assigment
Now that you know how to get user input and branch your program using conditional logic the code can move away from hardcoded values and become interactive.
- Open
main.pyfile in oursimple-bookstoreproject directory. - Instead of hardcoding values (
booktore_name,book_title,book_quantity,book_price) we will ask the user to enter these values usinginput()function. Remember thatinput()always returns str so we must cast the string to desired type (book_quantityto int andbook_priceto float). Replace all of these variables hardcoded values toinput()function, asking the user to enter the data. - Now the system must add some contraints, printing the error message and exiting if any of the rules are broken. Add these below each of the inputs, so that check runs as soon as user enters something. Rules are as follows:
bookstore_namemust be between 3 and 20 characters longbook_namemust be between 3 and 15 characters longbook_quantitymust be positive integer (above0)book_pricemust be positive float (above0.00)
- System must now also calculate taxes that depend on the
book_price, so make another variablebook_taxand set it to0. Then calculate the tax using below rules, assign it tobook_taxand print out the tax applied.- If
book_priceis above5tax is3%(0.03) - If
book_priceis above10tax is5%(0.05) - If
book_priceis above18tax is10%(0.1)
- If
- Display inventory listing like this:
Welcome to WILLOW CREEK BOOKS inventory system
- - - - - - - - - - - - - - - - - - - -
CURRENT INVENTORY
- - - - - - - - - - - - - - - - - - - -
- Book: The Last Cartographer -> [price:$18.5, tax: 10%, copies: 3, available: True]
- - - - - - - - - - - - - - - - - - - -
Inventory total: $55.5 [VAT: $60.05]
- - - - - - - - - - - - - - - - - - - -
Thank you for using our inventory system. - Commit and push your changes.
Deepen Your Knowledge
- Learn more about Indentation in Python from this Real Python article
- Learn The Importance of Indentation from this Medium article.
What's Next
Now that our programs can take user input, calculate things and evaluate what to do on those calculations, we can jump into loops. These help us to run a peace of code multiple times without us repeating the code.