Day 8: If-Else & Conditions – Make Your Code Think!

 Day 8: If-Else & Conditions – Make Your Code Think!


Heyy, Day 8 is here! You’ve already crushed Variables, Operators, Strings, Lists, Tuples, Sets, and Dictionaries. Now, Day 8 is all about If-Else & Conditions – this is where your code starts making decisions like a human!

Think of it like a traffic light:

Red → Stop Green → Go Yellow → Slow down

In Python, it’s:

python
if condition:
    do this
elif other_condition:
    do that
else:
    do something else

Let’s make your code smart!


What Are Conditions? (10 mins)

Use comparison and logical operators to build conditions:

OperatorMeaning
==Equal to
!=Not equal
>, <, >=, <=Greater/Less than
and, or, notCombine conditions

Examples:

python
age = 20
score = 85

print(age >= 18)          # True
print(score > 90)         # False
print(age >= 18 and score >= 80)  # True

If-Elif-Else Structure (15 mins)

python
marks = 78

if marks >= 90:
    grade = "A+"
elif marks >= 80:
    grade = "A"
elif marks >= 70:
    grade = "B"
elif marks >= 60:
    grade = "C"
else:
    grade = "Fail"

print("Your grade:", grade)  # Your grade: B

Key Rules:

  • if → first condition
  • elif → “else if” (as many as you want)
  • else → runs if nothing else matches
  • Indentation matters! (4 spaces or 1 tab)

Try This: In day8.py:

python
temp = 30

if temp > 35:
    print("Too hot!")
elif temp > 25:
    print("Nice weather!")
elif temp > 15:
    print("A bit cool")
else:
    print("Freezing!")

Run: python day8.py → Output: Nice weather!


Nested If & Complex Conditions (15 mins)

python
age = 20
has_id = True
is_student = False

if age >= 18:
    if has_id:
        print("Welcome!")
    else:
        print("Need ID!")
else:
    if is_student:
        print("Student entry allowed")
    else:
        print("Too young!")

Logical Operators (cleaner way):

python
if age >= 18 and has_id:
    print("Welcome!")
elif age < 18 and is_student:
    print("Student entry allowed")
else:
    print("Access denied!")

Mini Project: Smart Login System (30–40 mins)

Let’s build a Smart Login System! It will:

  • Have a username & password
  • Check if correct
  • Give 3 attempts
  • Block after 3 wrong tries

Code (day8_login.py):

python
# Smart Login System
correct_user = "admin"
correct_pass = "python123"
max_attempts = 3
attempts = 0

print("=== Secure Login ===")

while attempts < max_attempts:
    username = input("Username: ")
    password = input("Password: ")
    
    if username == correct_user and password == correct_pass:
        print("Login successful! Welcome!")
        break
    else:
        attempts += 1
        remaining = max_attempts - attempts
        if remaining > 0:
            print(f"Wrong credentials! {remaining} attempts left.")
        else:
            print("Account locked! Too many wrong attempts.")

Run & Test:

text
=== Secure Login ===
Username: admin
Password: wrong
Wrong credentials! 2 attempts left.
Username: admin
Password: python123
Login successful! Welcome!

Challenge: Add case-insensitive username:

python
if username.lower() == correct_user.lower() and password == correct_pass:

Bonus: Ternary Operator (One-Liner If)

python
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)  # Adult

Common Mistakes & Fixes

MistakeErrorFix
= instead of ==Logic errorUse == for comparison
Wrong indentationIndentationErrorKeep 4 spaces
No elseCode skipsAdd fallback
Infinite loopNever endsUse break or proper condition


Day 8 Wrap-Up

You learned:

  • if, elif, else structure
  • Comparison (==, >) & logical (and, or) operators
  • Nested conditions
  • Built a Smart Login System with attempt limit
  • Used while loop with break

Track Progress (in notebook):

Day 8 Done: Code now thinks! Built login system. Doubt: and vs or? Answer: and → both true; or → at least one true.

Resources:

Comments

Popular Posts