Gramin Technical and Management Campus
Department of computer engineering
Subject/code: -Python/22616
Name:-Pranusha Shiddhodhan Birhade
DOP: DOS:-
Practical no 16
1) Write a Python program to Check for ZeroDivisionError Exception
Code:-
def divide_numbers(num1, num2):
try:
result = num1 / num2
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
return None
else:
return result
if __name__ == "__main__":
try:
numerator = float(input("Enter the numerator: "))
denominator = float(input("Enter the denominator: "))
division_result = divide_numbers(numerator, denominator)
if division_result is not None:
print(f"The result of {numerator} divided by {denominator} is: {division_result}")
except ValueError:
print("Invalid input. Please enter numeric values.")
Output:-
2) Write a Python program to create user defined exception that will
check whether the password is correct or not?
Code:-
class PasswordError(Exception):
pass
def check_password(input_password, correct_password):
if input_password != correct_password:
raise PasswordError("Incorrect password! Please try again.")
if __name__ == "__main__":
correct_password = "SecurePassword123"
user_password = input("Enter your password: ")
try:
check_password(user_password, correct_password)
print("Password is correct!")
except PasswordError as e:
print(e)
Output:-