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

Jalaj Pandey Bca 5 Assignment 5

The document contains 5 questions related to Python programming. Question 1 defines a function to read a file, count vowels and total words. Question 2 defines a function to store user input data in a file after capitalizing it. Question 3 defines a function to read a poem file and count various elements like alphabets, spaces, lowercase/uppercase letters, specific words etc. Question 4 provides an explanation of exceptions in Python and how to handle them using try/except blocks. Question 5 defines a function to calculate inverse of a number entered by user and handles different exception types that may occur.

Uploaded by

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

Jalaj Pandey Bca 5 Assignment 5

The document contains 5 questions related to Python programming. Question 1 defines a function to read a file, count vowels and total words. Question 2 defines a function to store user input data in a file after capitalizing it. Question 3 defines a function to read a poem file and count various elements like alphabets, spaces, lowercase/uppercase letters, specific words etc. Question 4 provides an explanation of exceptions in Python and how to handle them using try/except blocks. Question 5 defines a function to calculate inverse of a number entered by user and handles different exception types that may occur.

Uploaded by

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

Jalaj Pandey

BCA 5th
Assignment 5

Que 1 :-
def readFile():
file1 = input("Enter the path: ")
file = open(file1, 'r')
lines = file.read()
count = 0
vowel = ['a','e','i','o','u']
for i in lines:
if i in vowel:
count +=1
words = lines.split()
print(f'total words are {len(words)}')
print(f'total vowles are {count}')

readFile()

Que 2 :-
def dataStore():
dataList = ""
while True:
data = input()
dataList += data + '\n'
if data == "":
break
dataCap = ""
for i in dataList:
dataCap += i.capitalize()
with open('file.txt', 'w+') as file:
file.write(dataCap)

dataStore()

Que 3 :-
def read_content():
poem = input("enter the path: ")
file = open(poem, 'r')
lines = file.read()
countAlpha = 0
countSpace = 0
countLower = 0
countUpper = 0
countNwV = 0
countBeautiful = 0
vowels = ['A','E','I','O','U','a','e','i','o','u']
for i in lines:
if (ord(i)>=97 and ord(i)<=122) or (ord(i)>=65 and ord(i)<=90):
countAlpha += 1
if i == " ":
countSpace += 1
if (ord(i)>=97 and ord(i)<= 122):
countLower += 1
if (ord(i)>=65 and ord(i)<=90):
countUpper += 1
words = lines.split()
for i in words:
if i == 'beautiful':
countBeautiful += 1
if i[0] in vowels:
countNwV += 1
print(f'''Alphabets are {countAlpha},
Spaces are {countSpace},
Lowercase letters are {countLower},
Uppercase letters are {countUpper},
beautiful words are {countBeautiful},
word starts with Vowels are {countNwV}
''')
read_content()
Que 4 :-

Exception- Python has many built-in exceptions that are raised when your program encounters an
error (something in the program goes wrong).

When these exceptions occur, the Python interpreter stops the current process and passes it to the
calling process until it is handled. If not handled, the program will crash.

Handling the Exception- Exceptions can be handled using a try statement.

The critical operation which can raise an exception is placed inside the try clause. The code that
handles the exceptions is written in the except clause.

We can thus choose what operations to perform once we have caught the exception. Here is a
simple example.

Syntax-
try:
  print(x)
except:
  print("Something went wrong")
finally:
  print("The 'try except' is finished")

Que 5 :-
def inverse1():
try:
num = input("Enter the number: ")
num = float(num)
inverse = 1.0 / num
except ValueError:
print("ValueError")
except TypeError:
print("TypeError")
except ZeroDivisionError:
print("ZeroDivisionError")
except:
print("Any other Error")
else:
print(inverse)
finally:
print("Function inverse Completed")

inverse1()

You might also like