Wednesday, December 5, 2018

Python Ninja Bootcamp 30-Q&A 5th


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

    Q. Write Program to construct a handle the divide by zero exception.

     
    try :
        1/0
    except Exception as e:
        print(e)
    
    >>division by zero

    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()
    
    >>
    Enter the number=string
    invalid literal for int() with base 10: 'string'
    Enter the number=2
    2 is the even number
     
     
    EDITS ARE WELCOMED!!

    In the next Blog, we will discuss decorator. 

    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-_1R1xnw

    Instagram-
    https://www.instagram.com/python.india/


    View the Jupyter Notebook for this lecture

    Download the Jupyter Notebook for this lecture 




    Feel free contact me on-


    No comments:

    Post a Comment