Learn Python like a Professional! Take You from 0 to Hero
In this Python lecture, we will learn about the Q&A fifth.
Answer the following Question-
Q. Define try expect else finally clause in the terms of Exception Handling
1. try -
2. except -
3. else -
4. finally -
Hint: Watch the previous video
1. try -
2. except -
3. else -
4. finally -
Hint: Watch the previous video
Q. Write Program to construct a handle the divide by zero exception.
try :
1/0
except Exception as e:
print(e)
Q.Write a program to create the function which takes user input and check whether the number is even or odd. If the input is not an integer, the error should be raised and handled by try-except else finally clause. Also ask the user to input again unless we get input as an integer.
def even_odd():
while(True):
try:
n=int(input('Enter the number='))
except Exception as e:
print(e)
else:
if n%2==0:
print('{x} is the even number'.format(x=n))
else:
print('{x} is the odd number'.format(x=n))
break
even_odd()
EDITS ARE WELCOMED!!
In the next Blog, we will discuss decorator.
https://sngurukuls247.blogspot.com/2018/12/python-ninja-bootcamp-31-decorators.html
......................................................................................................................................
Instagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
Download the Jupyter Notebook for this lecture
https://sngurukuls247.blogspot.com/2018/12/python-ninja-bootcamp-31-decorators.html
......................................................................................................................................
Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnwInstagram-
https://www.instagram.com/python.india/
View the Jupyter Notebook for this lecture
No comments:
Post a Comment