Skip to main content
Welcome back. Your dashboard can now take input, store it, and repeat it back to you. The next step is letting the program choose different behavior depending on inputs — that’s what if-statements do. In this lesson you will learn to:
  • Write conditional logic using if, elif, and else.
  • Use comparison operators to check conditions.
  • Combine conditions with and and or.
  • Respond to user input with branching logic.
Think about the apps you use every day: Instagram shows your feed if you’re logged in, otherwise it shows a login screen. Netflix lets you play videos if you’re a subscriber, otherwise it shows payment options. Programs must make choices — and your dashboard is no different. Conceptually, if-statements let you branch program behavior. Simple examples:
  • If energy is high → show “Great energy!”
  • If energy is low → show “Time to rest.”
To make those decisions you compare values using comparison operators. Comparison operators A reminder: a single = assigns a value to a variable, e.g. age = 25.
A presenter on the right gestures in front of a slide that shows a cartoon person at a laptop and a callout labeled "Decisions." The slide explains the assignment operator "=" with the example "age = 25."
Basic comparisons in code Create a new file named if.py and try this minimal comparison:
Running this prints boolean results:
Comparisons evaluate to True or False, which is what if uses to decide whether to run a block of code. Using input(): strings vs numbers A common pitfall is that input() always returns a string. Comparing that string directly to an integer raises a TypeError. Example:
If you type 60 this will raise an error like:
A code editor window shows a short Python script and a terminal with a TypeError message. A person is pictured in the lower-right, gesturing as if explaining the code.
Convert input to an integer Convert the string returned by input() to an integer with int() so numeric comparisons work:
Now the comparisons will evaluate correctly.
If the user types something that isn’t a valid integer (for example, “sixty”), int(...) will raise a ValueError. To make input handling robust, either validate the input before converting or wrap the conversion in a try/except block and re-prompt the user until valid input is provided.
Robust integer parsing (example) Here’s a simple loop that keeps asking until the user supplies a valid integer:
First if statement and indentation Replace a direct comparison with an if statement. Python uses indentation (conventionally 4 spaces) to define the body of if, elif, and else blocks:
Most editors auto-indent after you type the colon; that indentation is required — missing it causes an IndentationError. For style guidance, see Python’s PEP 8 style guide. If-else for two choices To handle both true and false outcomes, use if…else:
Mood example (single-check) A simple mood check:
Note: that else assumes anything not "happy" should get the “Oh dear” response. To handle more than two possible moods, use elif. Using if, elif, else for multiple choices Use if, elif, and else to create multiple branches. Python evaluates conditions from top to bottom and executes the first true branch — so order matters. Example energy classification:
Validating mood with three branches A more explicit mood prompt that enforces expected inputs:
Combining conditions with and / or Use and when multiple conditions must all be true, or when at least one condition must be true. Examples:
Putting it all together — a simple dashboard This compact example combines mood, energy classification, a wellbeing summary, and a goals prompt. Save it as dash.py and run it to try different inputs.
Sample runs (interactive):
  • Enter happy and 75 → script acknowledges mood, classifies energy, and prints that your wellbeing is great.
  • Enter sad and 75 → mood branch prints the sad message, energy is classified, and the wellbeing summary reflects the high energy.
Common mistakes to avoid
  • Missing the colon (:) at the end of an if, elif, or else line — causes a SyntaxError.
  • Incorrect indentation or mixing tabs and spaces — causes an IndentationError.
  • Using a single equals (=) instead of double equals (==) for comparison — = assigns a value and using it in a conditional will raise a SyntaxError or behave unexpectedly.
A person stands beside a large screen displaying a code editor with Python code and a terminal showing a syntax error. The visible code highlights an if-statement about mood and energy.
Summary
  • if statements let your program make decisions by evaluating conditions that are True or False.
  • Comparison operators (==, !=, >, <, >=, <=) are used to build conditions.
  • Logical operators (and, or) combine multiple conditions into richer tests.
  • Chains of if → elif → else handle multiple possibilities; order matters because Python stops at the first true condition.
Your dashboard now stores input in variables, makes decisions with if statements, and responds based on user input. Next you’ll learn how to repeat actions efficiently using loops. Links and references

Watch Video