If Else Problems
If Else Problems
Write a program that asks for a person's age and determines which age group they belong to:
Example Input/Output:
2. Even or Odd
Write a program that checks if a number entered by the user is even or odd.
Example Input/Output:
Enter a number: 7
7 is odd.
Write a program that takes three numbers from the user and determines the largest.
Example Input/Output:
4. Grade Calculator
Write a program that takes a test score (0-100) as input and outputs the corresponding grade:
90-100: A
80-89: B
70-79: C
60-69: D
Below 60: F
Example Input/Output:
Write a program to check if a given year is a leap year. A year is a leap year if:
It is divisible by 4 and not divisible by 100, or
It is divisible by 400.
Example Input/Output:
Write a program that takes a number as input and checks whether it is positive, negative, or zero.
Example Input/Output:
Write a program that takes a single letter from the user and checks if it is a vowel ( a, e, i, o, u) or a
consonant.
Example Input/Output:
Enter a letter: a
a is a vowel.
8. Triangle Validity
Write a program to check if three given sides can form a triangle. A triangle is valid if the sum of any two
sides is greater than the third.
Example Input/Output:
Enter side 1: 3
Enter side 2: 4
Enter side 3: 5
The sides form a triangle.
9. Eligible to Vote
Write a program that asks for the user's age and checks if they are eligible to vote (18 years or older).
Example Input/Output:
Write a program that calculates the discount based on the total purchase amount:
No discount if the total is less than $50.
10% discount if the total is between $50 and $100.
20% discount if the total is more than $100.
Example Input/Output:
Write a program that takes two numbers and an operator (+, -, *, /) as input, then performs the
corresponding operation.
Example Input/Output:
SOLUTIONS
2. Even or Odd
number = int(input("Enter a number: "))
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
if letter in 'aeiou':
print(f"{letter} is a vowel.")
elif letter.isalpha():
print(f"{letter} is a consonant.")
else:
print("Invalid input.")
8. Triangle Validity
side1 = float(input("Enter side 1: "))
side2 = float(input("Enter side 2: "))
side3 = float(input("Enter side 3: "))
if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
print("The sides form a triangle.")
else:
print("The sides do not form a triangle.")
9. Eligible to Vote
age = int(input("Enter your age: "))
if operator == '+':
print(f"The result is: {num1 + num2}")
elif operator == '-':
print(f"The result is: {num1 - num2}")
elif operator == '*':
print(f"The result is: {num1 * num2}")
elif operator == '/':
if num2 != 0:
print(f"The result is: {num1 / num2}")
else:
print("Error: Division by zero.")
else:
print("Invalid operator.")