Write a Python program that takes an age as input and raises a custom exception if the age is less than 18. The exception message should be “Age should be at least 18.”
Example 1:
Input: 15 Output: "Error: Age should be at least 18."
Example 2:
Input: 20 Output: "Valid age."
Use the ‘raise’ keyword to raise an exception when the given condition is met.
def validate_age(age):
if age < 18:
raise Exception("Error: Age should be at least 18.")
else:
return "Valid age."
print(validate_age(15)) # Output: "Error: Age should be at least 18."
print(validate_age(20)) # Output: "Valid age."
Unlock AI & Data Science treasures. Log in!