In [ ]:
try:
numerator = 10
denominator = 0
result = numerator/denominator
print(result)
except:
print("Error: Denominator cannot be 0.")
finally:
print("This is finally block.")
In [ ]:
Another Solution:
try:
print('try block')
x=int(input('Enter a number: '))
y=int(input('Enter another number: '))
z=x/y
except ZeroDivisionError:
print("except ZeroDivisionError block")
print("Division by 0 not accepted")
else:
print("else block")
print("Division = ", z)
finally:
print("finally block")
x=0
y=0
print ("Out of try, except, else and finally blocks." )
In [ ]:
one = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
two = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
both=[]
if len(one) < len(two):
for i in one:
if i in two and i not in both:
both.append(i)
if len(one) > len(two):
for i in two:
if i in one and i not in both:
both.append(i)
print(both)
In [ ]:
fname = input("Enter file name: ")
word=input("Enter word to be searched:")
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
if(i==word):
k=k+1
print("Occurrences of the word:")
print(k)
0 Comments:
Post a Comment