Skip to content

Commit d6985a2

Browse files
committed
Udemy Homework Errors and exception handling
1 parent 20843f8 commit d6985a2

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"kernelspec": {
6+
"display_name": "Python 3",
7+
"language": "python",
8+
"name": "python3"
9+
},
10+
"language_info": {
11+
"codemirror_mode": {
12+
"name": "ipython",
13+
"version": 3
14+
},
15+
"file_extension": ".py",
16+
"mimetype": "text/x-python",
17+
"name": "python",
18+
"nbconvert_exporter": "python",
19+
"pygments_lexer": "ipython3",
20+
"version": "3.6.6"
21+
},
22+
"colab": {
23+
"name": "02-Errors and Exceptions Homework.ipynb",
24+
"provenance": [],
25+
"include_colab_link": true
26+
}
27+
},
28+
"cells": [
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {
32+
"id": "view-in-github",
33+
"colab_type": "text"
34+
},
35+
"source": [
36+
"<a href=\"https://colab.research.google.com/github/DebjitHore/Python-Codes/blob/master/07-Errors%20and%20Exception%20Handling/02-Errors%20and%20Exceptions%20Homework.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {
42+
"id": "Eu6u0mhaw9-0"
43+
},
44+
"source": [
45+
"___\n",
46+
"\n",
47+
"<a href='https://www.udemy.com/user/joseportilla/'><img src='https://github.com/Pierian-Data/Complete-Python-3-Bootcamp/blob/master/Pierian_Data_Logo.png?raw=1'/></a>\n",
48+
"___\n",
49+
"<center><em>Content Copyright by Pierian Data</em></center>"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {
55+
"id": "u3PN3Egnw9-3"
56+
},
57+
"source": [
58+
"# Errors and Exceptions Homework"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {
64+
"id": "eEfcpWKww9-4"
65+
},
66+
"source": [
67+
"### Problem 1\n",
68+
"Handle the exception thrown by the code below by using <code>try</code> and <code>except</code> blocks."
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"metadata": {
74+
"id": "_bDUFy3Nw9-4",
75+
"outputId": "a47dc15f-7526-476d-8824-3e2c765e2c17",
76+
"colab": {
77+
"base_uri": "https://localhost:8080/"
78+
}
79+
},
80+
"source": [
81+
"try:\n",
82+
" for i in ['a','b','c']:\n",
83+
" print(i**2)\n",
84+
"except:\n",
85+
" print(\"Operation not supported\")"
86+
],
87+
"execution_count": 2,
88+
"outputs": [
89+
{
90+
"output_type": "stream",
91+
"text": [
92+
"Operation not supported\n"
93+
],
94+
"name": "stdout"
95+
}
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {
101+
"id": "2gOYOvMWw9-5"
102+
},
103+
"source": [
104+
"### Problem 2\n",
105+
"Handle the exception thrown by the code below by using <code>try</code> and <code>except</code> blocks. Then use a <code>finally</code> block to print 'All Done.'"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"metadata": {
111+
"id": "GyBbcq_tw9-5",
112+
"outputId": "0a1f092a-2ee5-47fa-9e4e-578cb71b4af7",
113+
"colab": {
114+
"base_uri": "https://localhost:8080/"
115+
}
116+
},
117+
"source": [
118+
"x = 5\n",
119+
"y = 0\n",
120+
"try:\n",
121+
" z = x/y\n",
122+
"except:\n",
123+
" print(\"Math Error\")\n",
124+
"finally:\n",
125+
" print(\"All Done!!\")"
126+
],
127+
"execution_count": 3,
128+
"outputs": [
129+
{
130+
"output_type": "stream",
131+
"text": [
132+
"Math Error\n",
133+
"All Done!!\n"
134+
],
135+
"name": "stdout"
136+
}
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {
142+
"id": "vEnshVL8w9-5"
143+
},
144+
"source": [
145+
"### Problem 3\n",
146+
"Write a function that asks for an integer and prints the square of it. Use a <code>while</code> loop with a <code>try</code>, <code>except</code>, <code>else</code> block to account for incorrect inputs."
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"metadata": {
152+
"collapsed": true,
153+
"id": "9LJP0Ojpw9-6"
154+
},
155+
"source": [
156+
"def ask():\n",
157+
" while True:\n",
158+
" try:\n",
159+
" num=int(input(\"Enter a number of your choice\"))\n",
160+
" print(num**2)\n",
161+
" except:\n",
162+
" print(\"Kindly recheck your input and enter an INTEGER\")\n",
163+
" else:\n",
164+
" print(\"Successful\")\n",
165+
" break\n",
166+
"\n",
167+
" "
168+
],
169+
"execution_count": 4,
170+
"outputs": []
171+
},
172+
{
173+
"cell_type": "code",
174+
"metadata": {
175+
"id": "Q9J_FnbZw9-6",
176+
"outputId": "79fce2fc-2c58-412d-a3da-48fdbe1e6105",
177+
"colab": {
178+
"base_uri": "https://localhost:8080/"
179+
}
180+
},
181+
"source": [
182+
"ask()"
183+
],
184+
"execution_count": 5,
185+
"outputs": [
186+
{
187+
"output_type": "stream",
188+
"text": [
189+
"Enter a number of your choicenull\n",
190+
"Kindly recheck your input and enter an INTEGER\n",
191+
"Enter a number of your choice2\n",
192+
"4\n",
193+
"Successful\n"
194+
],
195+
"name": "stdout"
196+
}
197+
]
198+
},
199+
{
200+
"cell_type": "markdown",
201+
"metadata": {
202+
"id": "LmWVd1EUw9-6"
203+
},
204+
"source": [
205+
"# Great Job!"
206+
]
207+
}
208+
]
209+
}

0 commit comments

Comments
 (0)