Skip to main content

Command Palette

Search for a command to run...

Python Conditionals: 10 Problems Solved

Published
•4 min read•View as Markdown

Conditionals in Python allow you to execute different blocks of code based on certain conditions being met. In this article, we'll explore 10 problems that can be solved using conditionals in Python.

1. Age Group Categorization

The first problem we'll tackle is classifying a person's age group. We'll categorize individuals into four groups: Child (<13), Teenager (13-19), Adult (20-59), and Senior (60+). Here's how we can solve this using Python:

age = 20

if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
elif age < 60:
    print("Adult")
else:
    print("Senior")

2. Movie Ticket Pricing

Next, let's consider a problem where movie tickets are priced based on age. Adults (18 and over) pay $12, while children pay $8. Additionally, there's a $2 discount on Wednesdays. Here's how we can calculate the ticket price:

age = 21
day = "Wednesday"

price = 12 if age >= 18 else 8

if day == "Wednesday":
    price -= 2

print("Your Ticket Price is:", price)

3. Grade Calculator

In this problem, we'll assign a letter grade based on a student's score. The grading scale is as follows: A (90-100), B (80-89), C (70-79), D (60-69), and F (below 60). Here's how we can implement this:

marks = 85

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

print("Grade is:", grade)

4. Fruit Ripeness Checker

In this problem, we'll determine if a fruit is ripe, overripe, or unripe based on its color. For example, a green banana is unripe, a yellow banana is ripe, and a brown banana is overripe. Here's how we can solve this:

fruit_color = "Green"

if fruit_color == "Green":
    print("Unripe")
elif fruit_color == "Yellow":
    print("Ripe")
elif fruit_color == "Brown":
    print("Overripe")

5. Weather Activity Suggestion

Next, let's suggest an activity based on the weather. For example, if it's sunny, we can suggest going for a walk. If it's rainy, we can suggest reading a book. And if it's snowy, we can suggest building a snowman. Here's how we can implement this:

weather = "Rainy"

if weather == "Sunny":
    print("Go for a walk")
elif weather == "Rainy":
    print("Read a book")
elif weather == "Snowy":
    print("Build a snowman")

6. Transportation Mode Selection

In this problem, we'll choose a mode of transportation based on the distance. For short distances (< 3km), we'll suggest walking. For medium distances (3-15 km), we'll suggest biking. And for long distances (> 15km), we'll suggest driving. Here's how we can solve this:

distance = 20

if distance < 3:
    print("Walk")
elif distance <= 15:
    print("Go in Bike")
else:
    print("Go in Car")

7. Coffee Customization

In this problem, we'll customize a coffee order. The options are "Small", "Medium", or "Large", with an option for an "Extra shot" for espresso. Here's how we can implement this:

order_size = "Large"
extra_shot = False

if extra_shot:
    coffee = order_size + " coffee with an espresso"
else:
    coffee = order_size + " coffee"

print(coffee)

8. Password Strength Checker

In this problem, we'll check the strength of a password. We'll classify passwords as "Weak" if they're less than 6 characters, "Medium" if they're 6-10 characters, and "Strong" if they're more than 10 characters. Here's how we can solve this:

password = "secure@5"
password_length = len(password)

if password_length < 6:
    print("Weak password")
elif password_length <= 10:
    print("Medium password")
else:
    print("Strong password")

9. Leap Year Checker

In this problem, we'll determine if a given year is a leap year. Leap years are divisible by 4, but not by 100 unless they're also divisible by 400. Here's how we can implement this:

year = 2024

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

10. Pet Food Recommendation

In this problem, we'll recommend a type of pet food based on the pet's species and age. For example, for dogs under 2 years old, we'll recommend puppy food. For cats over 5 years old, we'll recommend senior cat food. Here's how we can solve this:

pet = "cat"
age = 10

if pet == "dog" and age <= 2:
    print("Puppy food is recommended")
elif pet == "cat" and age >= 5:
    print("Senior cat food is recommended")

In the next article we will solve 10 problems on loops till then Happy Coding

More from this blog

Hruthik's Blog

16 posts