|
25 | 25 | "- Input: 32 Output: \"32 is divisible by 2\" \n",
|
26 | 26 | "- Input: 25 Output: \"25 is divisible by 5\"\n",
|
27 | 27 | "- Input: 40 Output: \"40 is divisible by 2 and 5\"\n",
|
28 |
| - "- Input: 77 Output: \"77 is not divisible by 2 or 5\"" |
| 28 | + "- Input: 77 Output: \"77 is not divisible by 2 nor 5\"" |
29 | 29 | ]
|
30 | 30 | },
|
31 | 31 | {
|
32 | 32 | "cell_type": "markdown",
|
33 | 33 | "metadata": {},
|
34 | 34 | "source": [
|
35 |
| - "### Possible Solution:" |
| 35 | + "### Possible Solutions:" |
36 | 36 | ]
|
37 | 37 | },
|
38 | 38 | {
|
39 | 39 | "cell_type": "code",
|
40 |
| - "execution_count": 16, |
| 40 | + "execution_count": 1, |
41 | 41 | "metadata": {},
|
42 | 42 | "outputs": [
|
43 | 43 | {
|
44 | 44 | "name": "stdout",
|
45 | 45 | "output_type": "stream",
|
46 | 46 | "text": [
|
47 |
| - "Choose an integer: 14\n" |
| 47 | + "Choose an integer: 14\n", |
| 48 | + "14 is divisible by 2\n" |
48 | 49 | ]
|
49 |
| - }, |
50 |
| - { |
51 |
| - "data": { |
52 |
| - "text/plain": [ |
53 |
| - "True" |
54 |
| - ] |
55 |
| - }, |
56 |
| - "execution_count": 16, |
57 |
| - "metadata": {}, |
58 |
| - "output_type": "execute_result" |
59 | 50 | }
|
60 | 51 | ],
|
61 | 52 | "source": [
|
62 | 53 | "x = int(input(\"Choose an integer: \"))\n",
|
63 |
| - "a = x/2\n", |
64 |
| - "b=x//2\n", |
65 |
| - "a==b\n" |
| 54 | + "if x%2==0 and x%5==0:\n", |
| 55 | + " print(f\"{x} is divisible by 2 and 5\")\n", |
| 56 | + "elif x%2==0:\n", |
| 57 | + " print(f\"{x} is divisible by 2\")\n", |
| 58 | + "elif x%5==0:\n", |
| 59 | + " print(f\"{x} is divisible by 5\")\n", |
| 60 | + "else:\n", |
| 61 | + " print(f\"{x} is not divisible by 2 nor 5\") " |
66 | 62 | ]
|
67 | 63 | },
|
68 | 64 | {
|
69 | 65 | "cell_type": "code",
|
70 |
| - "execution_count": null, |
| 66 | + "execution_count": 3, |
71 | 67 | "metadata": {},
|
72 |
| - "outputs": [], |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "Choose an integer: 77\n", |
| 74 | + "77 is not divisible by 2 or 5\n" |
| 75 | + ] |
| 76 | + } |
| 77 | + ], |
73 | 78 | "source": [
|
74 |
| - "a=5\n", |
75 |
| - "b=2" |
| 79 | + "x = int(input(\"Choose an integer: \"))\n", |
| 80 | + "if x%2==0:\n", |
| 81 | + " if x%5==0:\n", |
| 82 | + " print(f\"{x} is divisible by 2 and 5\")\n", |
| 83 | + " else:\n", |
| 84 | + " print(f\"{x} is divisible by 2\")\n", |
| 85 | + "elif x%5==0:\n", |
| 86 | + " print(f\"{x} is divisible by 5\")\n", |
| 87 | + "else:\n", |
| 88 | + " print(f\"{x} is not divisible by 2 nor 5\") " |
76 | 89 | ]
|
77 | 90 | },
|
78 | 91 | {
|
|
110 | 123 | },
|
111 | 124 | {
|
112 | 125 | "cell_type": "code",
|
113 |
| - "execution_count": null, |
| 126 | + "execution_count": 9, |
114 | 127 | "metadata": {},
|
115 |
| - "outputs": [], |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "name": "stdout", |
| 131 | + "output_type": "stream", |
| 132 | + "text": [ |
| 133 | + "What is your name: Navau\n", |
| 134 | + "What is your age: 30\n", |
| 135 | + "What is your favourite number: 2\n", |
| 136 | + "What is the number you hate the most: 7\n", |
| 137 | + "The last letter of your name is u. You are an adult. Your favorite number is smaller than your hated number.\n" |
| 138 | + ] |
| 139 | + } |
| 140 | + ], |
116 | 141 | "source": [
|
117 |
| - "age = \"un adulto\" if data_list[1]>=18 else if age<=3 \"bebe\" else \"menor\"" |
| 142 | + "data_list = [] #Create an empty list\n", |
| 143 | + "\n", |
| 144 | + "data_list.append(input(\"What is your name: \"))\n", |
| 145 | + "data_list.append(int(input(\"What is your age: \")))\n", |
| 146 | + "data_list.append(int(input(\"What is your favourite number: \")))\n", |
| 147 | + "data_list.append(int(input(\"What is the number you hate the most: \")))\n", |
| 148 | + "\n", |
| 149 | + "if data_list[1]>=18:\n", |
| 150 | + " age_category = \"an adult\"\n", |
| 151 | + "else:\n", |
| 152 | + " age_category = \"a minor\"\n", |
| 153 | + "\n", |
| 154 | + "if data_list[2]>data_list[3]:\n", |
| 155 | + " n = \"Your favorite number is bigger than your hated number\"\n", |
| 156 | + " \n", |
| 157 | + "elif data_list[2]<data_list[3]:\n", |
| 158 | + " n = \"Your favorite number is smaller than your hated number\"\n", |
| 159 | + "else:\n", |
| 160 | + " n = \"You hate your favorite number\"\n", |
| 161 | + " \n", |
| 162 | + "print(f\"The last letter of your name is {data_list[0][-1]}. You are {age_category}. {n}.\")" |
118 | 163 | ]
|
119 | 164 | },
|
120 | 165 | {
|
121 |
| - "cell_type": "code", |
122 |
| - "execution_count": null, |
| 166 | + "cell_type": "markdown", |
123 | 167 | "metadata": {},
|
124 |
| - "outputs": [], |
125 | 168 | "source": [
|
126 |
| - "if data_list[1]>=18:\n", |
127 |
| - " age=\"un adulto\"\n", |
128 |
| - "else:\n", |
129 |
| - " age=\"menor\"" |
| 169 | + "#### Lifehack\n", |
| 170 | + "Note there is a way to write single line if-elif-elses. The following are equivalent, but one of them is really compact!" |
130 | 171 | ]
|
131 | 172 | },
|
132 | 173 | {
|
133 | 174 | "cell_type": "code",
|
134 |
| - "execution_count": null, |
| 175 | + "execution_count": 7, |
135 | 176 | "metadata": {},
|
136 |
| - "outputs": [], |
| 177 | + "outputs": [ |
| 178 | + { |
| 179 | + "name": "stdout", |
| 180 | + "output_type": "stream", |
| 181 | + "text": [ |
| 182 | + "What is your age? 5\n", |
| 183 | + "minor\n" |
| 184 | + ] |
| 185 | + } |
| 186 | + ], |
137 | 187 | "source": [
|
138 |
| - "lista.append(1,2,3)" |
| 188 | + "age = int(input(\"What is your age? \"))\n", |
| 189 | + "\n", |
| 190 | + "age_category = \"adult\" if age>=18 else \"baby\" if age<=3 else \"minor\"\n", |
| 191 | + "\n", |
| 192 | + "print(age_category)" |
139 | 193 | ]
|
140 | 194 | },
|
141 | 195 | {
|
142 | 196 | "cell_type": "code",
|
143 |
| - "execution_count": 11, |
| 197 | + "execution_count": 8, |
144 | 198 | "metadata": {},
|
145 | 199 | "outputs": [
|
146 | 200 | {
|
147 | 201 | "name": "stdout",
|
148 | 202 | "output_type": "stream",
|
149 | 203 | "text": [
|
150 |
| - "What is your name: John\n", |
151 |
| - "What is your age: 43\n", |
152 |
| - "What is your favorite number: 7\n", |
153 |
| - "What number you hate the most: 2\n", |
154 |
| - "The last letter of your name is n. You are an adult. Your favorite number is bigger than your hated number.\n" |
| 204 | + "What is your age? 5\n", |
| 205 | + "minor\n" |
155 | 206 | ]
|
156 | 207 | }
|
157 | 208 | ],
|
158 | 209 | "source": [
|
159 |
| - "data_list = [] #Create an empty list\n", |
| 210 | + "age = int(input(\"What is your age? \"))\n", |
160 | 211 | "\n",
|
161 |
| - "data_list.append(input(\"What is your name: \"))\n", |
162 |
| - "data_list.append(int(input(\"What is your age: \")))\n", |
163 |
| - "data_list.append(int(input(\"What is your favorite number: \")))\n", |
164 |
| - "data_list.append(int(input(\"What number you hate the most: \")))\n", |
| 212 | + "if age>=18:\n", |
| 213 | + " age_category = \"adult\"\n", |
| 214 | + "elif age<=3:\n", |
| 215 | + " age_category = \"baby\"\n", |
| 216 | + "else:\n", |
| 217 | + " age_category = \"minor\"\n", |
| 218 | + "age_category = \"adult\" if age>=18 else \"baby\" if age<=3 else \"minor\"\n", |
| 219 | + "\n", |
| 220 | + "print(age_category)" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "markdown", |
| 225 | + "metadata": {}, |
| 226 | + "source": [ |
| 227 | + "## Exercise 3: The Magic Tricks\n", |
| 228 | + "### Trick A\n", |
| 229 | + "Print to the user four rectangles (made out of keyboard characters, e.g. using characters like `|`, `-` or `_`) that show the next numbers:\n", |
165 | 230 | "\n",
|
166 |
| - "age = \"an adult\" if data_list[1]>=18 else \"a minor\" #You can do ifs in one line like this\n", |
| 231 | + " **Box 1**:\n", |
| 232 | + " 8 9 10 11\n", |
| 233 | + " 12 13 14 15\n", |
167 | 234 | "\n",
|
168 |
| - "if data_list[2]>data_list[3]:\n", |
169 |
| - " n = \"Your favorite number is bigger than your hated number\"\n", |
170 |
| - " \n", |
171 |
| - "elif data_list[2]<data_list[3]:\n", |
172 |
| - " n = \"Your favorite number is smaller than your hated number\"\n", |
| 235 | + " **Box 2**:\n", |
| 236 | + " 4 5 6 7\n", |
| 237 | + " 12 13 14 15\n", |
| 238 | + "\n", |
| 239 | + " **Box 3**:\n", |
| 240 | + " 2 3 6 7\n", |
| 241 | + " 10 11 14 15\n", |
| 242 | + "\n", |
| 243 | + " **Box 4**:\n", |
| 244 | + " 1 3 5 7\n", |
| 245 | + " 9 11 13 15\n", |
| 246 | + "\n", |
| 247 | + "Ask them to think of those numbers and to give you the boxes in which it appears.\n", |
| 248 | + "\n", |
| 249 | + "You can guess it by adding the first number of the first row of each of those boxes!!!" |
| 250 | + ] |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "markdown", |
| 254 | + "metadata": {}, |
| 255 | + "source": [ |
| 256 | + "### Possible Solution" |
| 257 | + ] |
| 258 | + }, |
| 259 | + { |
| 260 | + "cell_type": "code", |
| 261 | + "execution_count": 30, |
| 262 | + "metadata": {}, |
| 263 | + "outputs": [ |
| 264 | + { |
| 265 | + "name": "stdout", |
| 266 | + "output_type": "stream", |
| 267 | + "text": [ |
| 268 | + "Hello, my dear! Let me guess the number you are thinking!\n", |
| 269 | + "\n", |
| 270 | + "Think of a number between 1 and 15...\n", |
| 271 | + "Now, let me draw some cards!\n", |
| 272 | + "\n", |
| 273 | + " Card 1\n", |
| 274 | + "---------------\n", |
| 275 | + "| 8 9 10 11 |\n", |
| 276 | + "| 12 13 14 15 |\n", |
| 277 | + "---------------\n", |
| 278 | + "\n", |
| 279 | + " Card 2\n", |
| 280 | + "---------------\n", |
| 281 | + "| 4 5 6 7 |\n", |
| 282 | + "| 12 13 14 15 |\n", |
| 283 | + "---------------\n", |
| 284 | + "\n", |
| 285 | + " Card 3\n", |
| 286 | + "---------------\n", |
| 287 | + "| 2 3 6 7 |\n", |
| 288 | + "| 10 11 14 15 |\n", |
| 289 | + "---------------\n", |
| 290 | + "\n", |
| 291 | + " Card 4\n", |
| 292 | + "---------------\n", |
| 293 | + "| 1 3 5 7 |\n", |
| 294 | + "| 9 11 13 15 |\n", |
| 295 | + "---------------\n", |
| 296 | + "\n", |
| 297 | + "Okay! Is it in box 1? (Answer Yes or No) yes\n", |
| 298 | + "\n", |
| 299 | + "Okay! Is it in box 2? (Answer Yes or No) no\n", |
| 300 | + "\n", |
| 301 | + "Okay! Is it in box 3? (Answer Yes or No) yes\n", |
| 302 | + "\n", |
| 303 | + "Okay! Is it in box 4? (Answer Yes or No) yes\n", |
| 304 | + "\n", |
| 305 | + "\n", |
| 306 | + "....Fish fish fish....\n", |
| 307 | + "Your number is 11\n" |
| 308 | + ] |
| 309 | + } |
| 310 | + ], |
| 311 | + "source": [ |
| 312 | + "print(\"Hello, my dear! Let me guess the number you are thinking!\\n\")\n", |
| 313 | + "print(\"Think of a number between 1 and 15...\\nNow, let me draw some cards!\\n\")\n", |
| 314 | + "print(\" Card 1\")\n", |
| 315 | + "print(\"-\"*15)\n", |
| 316 | + "print(f\"| {8:2} {9:2} {10:2} {11:2} |\")\n", |
| 317 | + "print(f\"| {12:2} {13:2} {14:2} {15:2} |\")\n", |
| 318 | + "print(\"-\"*15)\n", |
| 319 | + "print(\"\\n Card 2\")\n", |
| 320 | + "print(\"-\"*15)\n", |
| 321 | + "print(f\"| {4:2} {5:2} {6:2} {7:2} |\")\n", |
| 322 | + "print(f\"| {12:2} {13:2} {14:2} {15:2} |\")\n", |
| 323 | + "print(\"-\"*15)\n", |
| 324 | + "print(\"\\n Card 3\")\n", |
| 325 | + "print(\"-\"*15)\n", |
| 326 | + "print(f\"| {2:2} {3:2} {6:2} {7:2} |\")\n", |
| 327 | + "print(f\"| {10:2} {11:2} {14:2} {15:2} |\")\n", |
| 328 | + "print(\"-\"*15)\n", |
| 329 | + "print(\"\\n Card 4\")\n", |
| 330 | + "print(\"-\"*15)\n", |
| 331 | + "print(f\"| {1:2} {3:2} {5:2} {7:2} |\")\n", |
| 332 | + "print(f\"| {9:2} {11:2} {13:2} {15:2} |\")\n", |
| 333 | + "print(\"-\"*15)\n", |
| 334 | + "\n", |
| 335 | + "box1=input(\"\\nOkay! Is it in box 1? (Answer Yes or No) \")\n", |
| 336 | + "if box1==\"Yes\" or box1==\"yes\":\n", |
| 337 | + " box1=1\n", |
173 | 338 | "else:\n",
|
174 |
| - " n = \"You hate your favorite number\"\n", |
175 |
| - " \n", |
176 |
| - "print(f\"The last letter of your name is {data_list[0][-1]}. You are {age+1-49}. {n}.\")" |
| 339 | + " box1=0\n", |
| 340 | + "box2=input(\"\\nOkay! Is it in box 2? (Answer Yes or No) \")\n", |
| 341 | + "box2 = 2 if box2==\"Yes\" or box2==\"yes\" else 0\n", |
| 342 | + "box3=input(\"\\nOkay! Is it in box 3? (Answer Yes or No) \")\n", |
| 343 | + "box3 = 3 if box3==\"Yes\" or box3==\"yes\" else 0\n", |
| 344 | + "box4=input(\"\\nOkay! Is it in box 4? (Answer Yes or No) \")\n", |
| 345 | + "box4 = 4 if box4==\"Yes\" or box4==\"yes\" else 0\n", |
| 346 | + "\n", |
| 347 | + "card_first_elements = [0, 8, 4, 2, 1]\n", |
| 348 | + "the_number_is = card_first_elements[box1]+card_first_elements[box2]+card_first_elements[box3]+card_first_elements[box4]\n", |
| 349 | + "print(f\"\\n\\n....Fish fish fish....\\nYour number is {the_number_is}\")" |
| 350 | + ] |
| 351 | + }, |
| 352 | + { |
| 353 | + "attachments": {}, |
| 354 | + "cell_type": "markdown", |
| 355 | + "metadata": {}, |
| 356 | + "source": [ |
| 357 | + "### Trick B\n", |
| 358 | + "Using more or less the same code, you can now do the trick with more numbers! (Double impressive!)\n", |
| 359 | + "\n", |
| 360 | + " ** Box 1 **\n", |
| 361 | + " 1\t 3\t 5\t 7\t 9\t11\t13\t15\n", |
| 362 | + " 17\t19\t21\t23\t25\t27\t29\t31\n", |
| 363 | + " 33\t35\t37\t39\t41\t43\t45\t47\n", |
| 364 | + " 49\t51\t53\t55\t57\t59\t61\t63\n", |
| 365 | + "\n", |
| 366 | + " ** Box 2 **\n", |
| 367 | + " 2\t 3\t 6\t 7\t10\t11\t14\t15\n", |
| 368 | + " 18\t19\t22\t23\t26\t27\t30\t31\n", |
| 369 | + " 34\t35\t38\t39\t42\t43\t46\t47\n", |
| 370 | + " 50\t51\t54\t55\t58\t59\t62\t63\n", |
| 371 | + "\n", |
| 372 | + " ** Box 3 **\n", |
| 373 | + " 8\t 9\t10\t11\t12\t13\t14\t15\t\t\t\t\t\t\n", |
| 374 | + " 24\t25\t26\t27\t28\t29\t30\t31\n", |
| 375 | + " 40\t41\t42\t43\t44\t45\t46\t47\n", |
| 376 | + " 56\t57\t58\t59\t60\t61\t62\t63\n", |
| 377 | + "\n", |
| 378 | + " ** Box 4 **\n", |
| 379 | + " 16\t17\t18\t19\t20\t21\t22\t23\n", |
| 380 | + " 24\t25\t26\t27\t28\t29\t30\t31\n", |
| 381 | + " 48\t49\t50\t51\t52\t53\t54\t55\n", |
| 382 | + " 56\t57\t58\t59\t60\t61\t62\t63\n", |
| 383 | + "\n", |
| 384 | + " ** Box 5 **\n", |
| 385 | + " 4\t 5\t 6\t 7\t12\t13\t14\t15\n", |
| 386 | + " 20\t21\t22\t23\t28\t29\t30\t31\n", |
| 387 | + " 36\t37\t38\t39\t44\t45\t46\t47\n", |
| 388 | + " 52\t53\t54\t55\t60\t61\t62\t63\n" |
177 | 389 | ]
|
178 | 390 | }
|
179 | 391 | ],
|
|
0 commit comments