If Else Logic Programs
If Else Logic Programs
Here are 10 different scenarios using if, elif, and else statements in Python:
1. Grade Evaluation:
python
Copy code
grade = 85
if grade >= 90:
print("You got an A!")
elif grade >= 80:
print("You got a B.")
elif grade >= 70:
print("You got a C.")
else:
print("You need to improve.")
2. Age Check:
python
Copy code
age = 18
if age < 13:
print("You're a child.")
elif age < 20:
print("You're a teenager.")
else:
print("You're an adult.")
3. Temperature Response:
python
Copy code
temperature = 30
if temperature > 30:
print("It's hot outside.")
elif temperature > 20:
print("The weather is nice.")
else:
print("It's a bit cold.")
4. Traffic Light:
python
Copy code
traffic_light = "red"
if traffic_light == "green":
print("You can go.")
elif traffic_light == "yellow":
print("Get ready to stop.")
else:
print("You must stop.")
5. User Login:
python
Copy code
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Login successful!")
elif username != "admin":
print("Invalid username.")
else:
print("Invalid password.")
6. Shopping Cart:
python
Copy code
items_in_cart = 5
if items_in_cart == 0:
print("Your cart is empty.")
elif items_in_cart < 5:
print("You have a few items.")
else:
print("You have a full cart.")
7. Weather Warning:
python
Copy code
wind_speed = 50
if wind_speed > 60:
print("Severe weather warning!")
elif wind_speed > 30:
print("Windy conditions.")
else:
print("Calm weather.")
8. Fuel Check:
python
Copy code
fuel_level = 15
if fuel_level < 10:
print("Refuel soon!")
elif fuel_level < 30:
print("Fuel level is low.")
else:
print("Fuel level is good.")
9. Password Strength:
python
Copy code
password = "myp@ssw0rd"
if len(password) < 8:
print("Weak password.")
elif any(char.isdigit() for char in password) and any(char.isalpha() for
char in password):
print("Strong password.")
else:
print("Moderate password.")
python
Copy code
attendees = 20
if attendees < 10:
print("Consider canceling the event.")
elif attendees < 30:
print("The event will be small.")
else:
print("The event is well-attended!")