If Else Programs Advanced

1. Electricity Bill Calculator

units = int(input(“Enter electricity units consumed: “))

if units <= 100:
amount = units * 5
elif units <= 200:
amount = 100 * 5 + (units – 100) * 7
elif units <= 300:
amount = 100 * 5 + 100 * 7 + (units – 200) * 10
else:
amount = 100 * 5 + 100 * 7 + 100 * 10 + (units – 300) * 15

print(“Total electricity bill: ₹”, amount)

2. ATM Withdrawal Logic

balance = 10000
amount = int(input(“Enter amount to withdraw: “))

if amount % 100 != 0:
print(“Please enter amount in multiples of 100.”)
elif amount > balance:
print(“Insufficient balance.”)
elif amount > 5000:
print(“Maximum withdrawal limit is ₹5000 at a time.”)
else:
balance -= amount
print(f”Please collect your cash. Remaining balance: ₹{balance}”)

3. Movie Ticket Pricing Based on Age and Time

age = int(input(“Enter your age: “))
time = int(input(“Enter time in 24-hr format (e.g., 13 for 1 PM): “))

if age < 5:
price = 0
elif age <= 18:
price = 100
elif age >= 60:
price = 80
else:
price = 150

# Add time-based discount
if 11 <= time <= 14:
price -= 20 # Matinee discount

print(“Your ticket price is: ₹”, price)

4. Traffic Fine Calculator

speed = int(input(“Enter vehicle speed (in km/h): “))
is_school_zone = input(“Is this a school zone? (yes/no): “).lower()

if speed <= 40:
print(“No fine.”)
elif speed <= 60:
fine = 100
elif speed <= 80:
fine = 300
else:
fine = 1000

if is_school_zone == “yes”:
fine *= 2

print(“Your fine is: ₹”, fine)

5. Three-Subject Result System

sub1 = int(input(“Enter marks of Subject 1: “))
sub2 = int(input(“Enter marks of Subject 2: “))
sub3 = int(input(“Enter marks of Subject 3: “))

if sub1 < 33 or sub2 < 33 or sub3 < 33:
print(“Result: Fail (one or more subjects below pass marks)”)
else:
avg = (sub1 + sub2 + sub3) / 3
if avg >= 75:
print(“Result: Distinction”)
elif avg >= 60:
print(“Result: First Division”)
elif avg >= 45:
print(“Result: Second Division”)
else:
print(“Result: Third Division”)

Python If Else Examples

Python

Conditional Statements ( If – Else )

 

  1. Check whether the number is positive or negative
    num = int(input(“Enter a number: “))
    if num >= 0:
    print(“The number is positive”)
    else:
    print(“The number is negative”)

 

  1. Check Whether the number is Odd or Even
    num = int(input(“Enter a number: “))
    if num % 2 == 0:
        print(“Even number”)
    else:
        print(“Odd number”)

 

  1. Check if a person is eligible to vote
    age = int(input(“Enter your age: “))
    if age >= 18:
        print(“You are eligible to vote”)
    else:
        print(“You are not eligible to vote”)

 

  1. Find the greatest of two numbers
    a = int(input(“Enter first number: “))
    b = int(input(“Enter second number: “))
    if a > b:
        print(“First number is greater”)
    else:
        print(“Second number is greater or equal”)

 

  1. Check if a number is divisible by 5
    num = int(input(“Enter a number: “))
    if num % 5 == 0:
        print(“The number is divisible by 5”)
    else:
        print(“The number is not divisible by 5”)

 

  1. Check whether a year is a leap year
    year = int(input(“Enter a year: “))
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        print(“Leap year”)
    else:
       print(“Not a leap year”)

 

  1. Check if a character is a vowel or consonant
    ch = input(“Enter a character: “).lower()
    if ch in ‘aeiou’:
        print(“Vowel”)
    else:
        print(“Consonant”)

 

  1. Simple grading system
    marks = int(input(“Enter marks: “))
    if marks >= 90:
        print(“Grade A”)
    elif marks >= 75:
        print(“Grade B”)
    elif marks >= 50:
        print(“Grade C”)
    else:
        print(“Grade D”)

 

  1. Check if a triangle is valid
    a = int(input(“Enter first side: “))
    b = int(input(“Enter second side: “))
    c = int(input(“Enter third side: “))
    if a + b > c and b + c > a and c + a > b:
        print(“Valid triangle”)
    else:
        print(“Invalid triangle”)

 

  1. Check whether the entered character is uppercase or lowercase
    ch = input(“Enter a character: “)
    if ch.isupper():
        print(“Uppercase letter”)
    elif ch.islower():
        print(“Lowercase letter”)
    else:
        print(“Not an alphabet letter”)