satya

Friday, January 1, 2021

Program 34

                                                                    Program 34
 Write a program to demonstrate try/finally and with/as.

try: 
   a=int(input("enter the A value:"))
   b=int(input("enter the B value:"))
   s=a/b
except ZeroDivisionError:    
    print("Can't divide by zero(b=0)")
else:
    print("The divsion is=",s) 
finally: 
    print('This is always executed') 


OUTPUT:
1.=== RESTART: C:/Users/crrcse/Desktop/36.py =====
enter the A value:20
enter the B value:0
Can't divide by zero(b=0)
This is always executed
>>> 

2.===== RESTART: C:/Users/crrcse/Desktop/36.py =======
enter the A value:20
enter the B value:2
The divsion is= 10.0
This is always executed
>>> 

with/as.


file=open('satya.txt','w')
try:
    file.write('welcome to python')
finally:
    file.close()
with open('satya2.txt','w')as file:
    file.write('welcome to python')


OUTPUT:














No comments:

Post a Comment

Program 30

                                                                         Program 30 Write a Python class to implement pow(x, n). class Pow1:...