From 72f34e29f1d06eb955ee908bebb78d7aefacdd41 Mon Sep 17 00:00:00 2001 From: Nupura Date: Tue, 22 Jun 2021 13:35:29 +0530 Subject: [PATCH 1/2] Errors assignment --- Errors_Exceptions.ipynb | 105 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Errors_Exceptions.ipynb diff --git a/Errors_Exceptions.ipynb b/Errors_Exceptions.ipynb new file mode 100644 index 0000000..3796820 --- /dev/null +++ b/Errors_Exceptions.ipynb @@ -0,0 +1,105 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "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": 24, + "id": "worse-chicago", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter number: m\n", + "Number is non 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", + "\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!')\n", + "else:\n", + " print('Number is numeric.')" + ] + } + ], + "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 +} From acc029c0ff9ee9cb8afb2aba474d8855a3b66290 Mon Sep 17 00:00:00 2001 From: Nupura Date: Tue, 22 Jun 2021 15:00:44 +0530 Subject: [PATCH 2/2] Error assign --- Errors_Exceptions.ipynb | 42 +++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/Errors_Exceptions.ipynb b/Errors_Exceptions.ipynb index 3796820..52009cc 100644 --- a/Errors_Exceptions.ipynb +++ b/Errors_Exceptions.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 3, + "execution_count": 26, "id": "affecting-trailer", "metadata": {}, "outputs": [ @@ -53,32 +53,46 @@ }, { "cell_type": "code", - "execution_count": 24, - "id": "worse-chicago", + "execution_count": 29, + "id": "agricultural-brunei", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Enter number: m\n", - "Number is non numeric!\n" + "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", - "\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!')\n", - "else:\n", - " print('Number is numeric.')" + "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": {