1.
Python program to calculate the quotient of two numbers:
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Calculate quotient
quotient = num1 / num2
print(f"The quotient of {num1} divided by {num2} is: {quotient}")
except ValueError:
print("Invalid input! Please enter valid numbers.")
except ZeroDivisionError:
print("Error! Division by zero is not allowed.")
2. Python program to read a file and display its contents:
try:
filename = input("Enter the file name: ")
with open(filename, 'r') as file:
content = [Link]()
print(content)
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
except IOError:
print(f"Error: There was a problem reading the file '{filename}'.")
3. Python program to calculate the average of a list of integers:
try:
user_input = input("Enter a list of integers separated by space: ")
numbers = list(map(int, user_input.split()))
# Calculate average
if len(numbers) == 0:
raise ValueError("The list is empty, cannot calculate average.")
average = sum(numbers) / len(numbers)
print(f"The average of the numbers is: {average}")
except ValueError as e:
print(f"Error: {e}")
except ZeroDivisionError:
print("Error! Cannot calculate average of an empty list.")
4. Python program to read a CSV file and display its contents:
import csv
try:
filename = input("Enter the CSV file name: ")
with open(filename, 'r') as file:
csv_reader = [Link](file)
for row in csv_reader:
print(row)
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
except IOError:
print(f"Error: There was a problem reading the file '{filename}'.")
except [Link]:
print("Error: There was a problem reading the CSV file.")
5. Python program to convert a string to an integer:
try:
user_input = input("Enter a string to convert to an integer: ")
integer_value = int(user_input)
print(f"The integer value is: {integer_value}")
except ValueError:
print("Error: Invalid input! The string could not be converted to an integer.")
6. Python program to find the maximum and minimum numbers in a list:
try:
user_input = input("Enter a list of numbers separated by space: ")
numbers = list(map(float, user_input.split()))
if len(numbers) == 0:
raise ValueError("The list is empty, cannot find the maximum and
minimum.")
max_num = max(numbers)
min_num = min(numbers)
print(f"The maximum number is: {max_num}")
print(f"The minimum number is: {min_num}")
except ValueError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
7. Python program to read a file and write its contents to another file:
try:
source_file = input("Enter the source file name: ")
destination_file = input("Enter the destination file name: ")
with open(source_file, 'r') as src, open(destination_file, 'w') as dest:
content = [Link]()
[Link](content)
print(f"Content successfully written to {destination_file}")
except FileNotFoundError:
print(f"Error: The file '{source_file}' was not found.")
except IOError:
print("Error: There was a problem reading or writing the files.")
8. Python program to concatenate two strings:
try:
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
concatenated = str1 + str2
print(f"The concatenated string is: {concatenated}")
except Exception as e:
print(f"An error occurred during string concatenation: {e}")
9. Python program to count the number of words in a text file:
try:
filename = input("Enter the text file name: ")
with open(filename, 'r') as file:
content = [Link]()
word_count = len([Link]())
print(f"The number of words in the file is: {word_count}")
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
except IOError:
print("Error: There was a problem reading the file.")
10. Python program to reverse a string:
try:
user_input = input("Enter a string to reverse: ")
reversed_string = user_input[::-1]
print(f"The reversed string is: {reversed_string}")
except Exception as e:
print(f"An error occurred while reversing the string: {e}")