diff --git a/Errors_Exceptions.ipynb b/Errors_Exceptions.ipynb new file mode 100644 index 0000000..52009cc --- /dev/null +++ b/Errors_Exceptions.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 26, + "id": "affecting-trailer", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This code is having \"TypeError\"\n" + ] + } + ], + "source": [ + "#Assignment 1: Handle the exception thrown by the code below by using try and except blocks.\n", + "try:\n", + " for i in ['a','b','c']:\n", + " print(i**2)\n", + "except TypeError:\n", + " print('This code is having \"TypeError\"')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "vulnerable-school", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This code is having \"ZeroDivisionError\"\n", + "All done\n" + ] + } + ], + "source": [ + "#Assignment 2: Handle the exception thrown by the code below by using try and except blocks. \n", + "#Then use a finally block to print 'All done'\n", + "try:\n", + " x=5\n", + " y=0\n", + " z=x/y\n", + "except ZeroDivisionError:\n", + " print('This code is having \"ZeroDivisionError\"')\n", + "finally:\n", + " print('All done')" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "agricultural-brunei", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter number: r\n", + "Number is non numeric! Try again!\n", + "Enter number: e\n", + "Number is non numeric! Try again!\n", + "Enter number: 4\n", + "Square:16\n", + "Number is numeric!\n" + ] + } + ], + "source": [ + "#Assignment 3: Write a function that asks for an integer and prints the square of it. \n", + "#use a while loop with a try,except,else block to account for incorrect inputs.\n", + "while True:\n", + " try:\n", + " num=int(input('Enter number: '))\n", + " num_square=num**2\n", + " print('Square:{}'.format(num_square))\n", + " except:\n", + " print('Number is non numeric! Try again!')\n", + " else:\n", + " print('Number is numeric!')\n", + " break" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "large-prototype", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}