PYTHON
Errors and Built in Exception and Namespace and Scope
Fill in the blanks
1.a=2,b=2.print('a=',id(a))= 505910976. print('b=',id(b)) = 505910976.
2.a='hai'.b='hello'.c='hai'.id(a)=36027328.print(id(c))= 36027328.
3.Collections of names is called namespace.
Answer the following questions
(4 marks)
1.Write a program for syntaxError, ZeroDivisionError in Error handling
try:
N1,N2=eval(input("enter 2
numbers,seperated by comma:"))
res=N1/N2
except ZeroDivisionerror:
print("division
by zero is error!!")
res=0
except Syntaxerror:
print("comma is
missing.Input Value seperated by comma like this 3,4")
#all other exception
except:
print("Wrong
input")
else:
print("no
exception")
finally:
print("N1
is{},N2 is{},Result is{}".format(N1,N2,res))
2.Write a program to find positive or negative number in Error handling
class MyError(Exception):
pass
class ZeroValueError(Exception):
pass
while True:
try:
i_num=int(input("enter a number:"))
if i_num==0:
raise ZeroValueError
if i_num<0:
raise MyError
else:
print("valid input")
break
except MyError:
print("value should not be negative number........,try
again!")
except
ZeroValueError:
print("value should not be Zero........,try
again!")
Answer the following questions
(10 marks)
1.Write a program for Guess the number in Error handling
class Error(Exception):
pass
class ValueTooSmallError(Error):
pass
class ValueTooLargeError(Error):
pass
while True:
try:
i_num=int(input("enter a number:"))
number=10
if i_num<number:
raise ValueTooSmallError
elif i_num>number:
raise ValueTooLargeError
break
except
ValueTooSmallError:
print("this value is too small,try again!")
print()
except
ValueTooLargeError:
print("this value is too large,try again!")
print()
print("Congratulations!You guessed it
correctly.")
No comments:
Post a Comment