02 - Programming Day 08 - Conditional Tests and If Statements
02 - Programming Day 08 - Conditional Tests and If Statements
Conditional Statements
Introduction
In our daily lives, we often make actions according to how different conditions play
out in a given situation. The conditions determine what actions we will take.
We’ve already been briefly introduced to this when studying our Introduction to
Programming section, but we’ll now focus on doing this in Python.
Flowchart Python
Conditional Statements
Introduction
In programming, we look at a variety of conditions in order to help make
decisions. These are typically called conditional statements. Similarly, in Python
a Boolean expression is when something is either True or False.
NOTE: You can add multiple lines of code in the indented section as
much
as you need for your program.
If Statements
Type #2: If-Else Statements
Sometimes you need to do one action for one condition but something else for
all other conditions.
Your admission
OR cost is $25.
If Statements
Type #4: Multiple elif statements
You can use as many elif blocks that you need for your given program.
2. Stages of Life: Write an if-elif-else chain that determines a person’s stage of life. Set a value for the variable age, and then:
• If the person is less than 2 years old, print a message that the person is a baby.
• If the person is at least 2 years old but less than 4, print a message that the person is a toddler.
• If the person is at least 4 years old but less than 13, print a message that the person is a kid.
• If the person is at least 13 years old but less than 20, print a message that the person is a teenager.
• If the person is at least 20 years old but less than 65, print a message that the person is an adult.
• If the person is age 65 or older, print a message that the person is an elder.
3. Checking Usernames: Create a program that simulates how websites ensure that everyone has a unique username using
the following steps:
• Make a list of five or more usernames called current_users.
• Make another list of five usernames called new_users. Make sure two of the new usernames are also in the
current_users list.
• Using a for loop, loop through the new_users list to see if each new username has already been used. If it has, print a
message that the person will need to enter a new username. If a username has not been used, print a message saying
that the username is available.
• Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.
(To do this, you’ll need to make a copy of current_users containing the lowercase versions of all existing users.)