Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

Python

The document contains multiple Python functions demonstrating error handling through try-except blocks. Each function addresses different scenarios such as input validation, list access, dictionary lookup, age validation, factorial computation, and grade processing. The use of exceptions like ValueError, IndexError, and TypeError is highlighted to manage invalid inputs and ensure robust code execution.

Uploaded by

Tech- 420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Python

The document contains multiple Python functions demonstrating error handling through try-except blocks. Each function addresses different scenarios such as input validation, list access, dictionary lookup, age validation, factorial computation, and grade processing. The use of exceptions like ValueError, IndexError, and TypeError is highlighted to manage invalid inputs and ensure robust code execution.

Uploaded by

Tech- 420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1)

def get_integer():

try:

user_input = input("Enter an integer: ")

value = int(user_input)

print(f"You entered a valid integer: {value}")

except ValueError:

raise ValueError("Invalid input! You must enter a valid integer.")

try:

get_integer()

except ValueError as e:

print(e)

--------------------------------------------------------------------------------------------------------------

2)

def access_list_element():

try:

my_list = [10, 20, 30, 40, 50]

print("List:", my_list)

index = int(input("Enter the index of the element you want to access: "))

print(f"Element at index {index}: {my_list[index]}")

except IndexError:

print("Error: Index out of range! Please enter a valid index.")

except ValueError:

print("Error: Invalid input! Please enter an integer.")

access_list_element()
3)

def access_list_element():

my_list = [10, 20, 30, 40, 50]

print("List:", my_list)

try:

index = int(input("Enter the index of the element you want to access: "))

print(f"Element at index {index}: {my_list[index]}")

except IndexError:

print("Index out of range.")

except ValueError:

print("Invalid input! Please enter an integer.")

else:

print("Element accessed successfully.")

finally:

print("List access attempt completed.")

access_list_element()

--------------------------------------------------------------------------------------------------------------

4)

def dictionary_lookup():

my_dict = {"name": "Alice", "age": 25, "city": "New York", "job": "Engineer"}

print("Dictionary:", my_dict)

try:

key = input("Enter the key you want to look up: ")

value = my_dict[key]

except KeyError:
print("KeyError: The key does not exist in the dictionary.")

else:

print(f"The value for the key '{key}' is: {value}")

finally:

print("Dictionary lookup complete.")

dictionary_lookup()

--------------------------------------------------------------------------------------------------------------

5)

def validate_age():

try:

age = input("Enter your age: ")

if '.' in age:

raise TypeError("Age must be an integer, not a floating-point number.")

age = int(age)

if age < 0:

raise ValueError("Age cannot be negative.")

except ValueError as ve:

print(f"ValueError: {ve}")

except TypeError as te:

print(f"TypeError: {te}")

else:

print("Valid age entered.")

finally:

print("Age validation completed.")

validate_age()
6)

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n - 1)

def compute_factorial():

try:

num = input("Enter a number to calculate its factorial: ")

if not num.isdigit():

raise TypeError("Input must be an integer.")

num = int(num)

if num < 0:

raise ValueError("Factorial is not defined for negative numbers.")

result = factorial(num)

except ValueError as ve:

print(f"ValueError: {ve}")

except TypeError as te:

print(f"TypeError: {te}")

else:

print(f"The factorial of {num} is: {result}")

finally:

print("Factorial computation completed.")

compute_factorial()
7)

def process_marks():

try:

marks = float(input("Enter student marks (0-100): "))

if marks < 0 or marks > 100:

raise ValueError("Marks must be between 0 and 100.")

if marks >= 90:

grade = "A"

elif marks >= 80:

grade = "B"

elif marks >= 70:

grade = "C"

elif marks >= 60:

grade = "D"

else:

grade = "F"

except ValueError as ve:

print(f"ValueError: {ve}")

else:

print(f"The grade for marks {marks} is: {grade}")

finally:

print("Marks processing completed.")

process_marks()

You might also like