|
246 | 246 | }, |
247 | 247 | { |
248 | 248 | "cell_type": "code", |
249 | | - "execution_count": null, |
| 249 | + "execution_count": 12, |
250 | 250 | "metadata": {}, |
251 | 251 | "outputs": [], |
252 | 252 | "source": [ |
253 | 253 | "# Test yourself\n", |
254 | 254 | "\n", |
255 | 255 | "# Write your own find_char function that takes a character and a string\n", |
| 256 | + "def find_char(character, statement):\n", |
| 257 | + " for i in range(len(statement)):\n", |
| 258 | + " statement[i]\n", |
| 259 | + " if character == statement[i]:\n", |
| 260 | + " return i\n", |
| 261 | + " \n", |
| 262 | + " return -1\n", |
| 263 | + " \n", |
| 264 | + "\n", |
| 265 | + "\n", |
256 | 266 | "\n", |
257 | 267 | "assert find_char('x', 'My fox likes bricks') == 5\n", |
258 | 268 | "assert find_char('z', 'There is a zebra in the garden') == 11\n", |
259 | 269 | "assert find_char('w', \"I've lost my shoes\") == -1\n", |
260 | 270 | "\n", |
261 | | - "find_char('')" |
| 271 | + "assert find_char('y', \"Sundaay\") == 6" |
262 | 272 | ] |
263 | 273 | }, |
264 | 274 | { |
|
300 | 310 | }, |
301 | 311 | { |
302 | 312 | "cell_type": "code", |
303 | | - "execution_count": null, |
| 313 | + "execution_count": 24, |
304 | 314 | "metadata": {}, |
305 | | - "outputs": [], |
| 315 | + "outputs": [ |
| 316 | + { |
| 317 | + "name": "stdout", |
| 318 | + "output_type": "stream", |
| 319 | + "text": [ |
| 320 | + "3\n", |
| 321 | + "1\n", |
| 322 | + "Different\n", |
| 323 | + "Same\n", |
| 324 | + "4\n", |
| 325 | + "6\n", |
| 326 | + "Different\n" |
| 327 | + ] |
| 328 | + } |
| 329 | + ], |
306 | 330 | "source": [ |
307 | 331 | "# Test Yourself\n", |
308 | 332 | "\n", |
309 | 333 | "# Write a function to add two numbers and return the result\n", |
310 | | - "\n", |
| 334 | + "def add(number1, number2):\n", |
| 335 | + " return number1 + number2\n", |
| 336 | + "print(add(1, 2))\n", |
311 | 337 | "# Write a function to subtract two numbers and return the result \n", |
312 | | - "\n", |
| 338 | + "def subtract(number3, number4):\n", |
| 339 | + " return number3 - number4\n", |
| 340 | + "print(subtract(4, 3))\n", |
313 | 341 | "# Write a function to print \"Same\" if two numbers are the same, otherwise print \"Different\"\n", |
314 | | - "\n", |
| 342 | + "def same(number5, number6):\n", |
| 343 | + " if number5 == number6:\n", |
| 344 | + " return \"Same\"\n", |
| 345 | + " else:\n", |
| 346 | + " return \"Different\"\n", |
| 347 | + "print(same(5, 6))\n", |
| 348 | + "print(same(5, 5))\n", |
315 | 349 | "# Use your functions to show that 2 + 2 = 4\n", |
316 | | - "\n", |
| 350 | + "print(add(2, 2))\n", |
317 | 351 | "# Use your functions to show that ( 5 + 3) - 2 = 6\n", |
318 | | - "\n", |
| 352 | + "print(subtract(add(5, 3), 2))\n", |
319 | 353 | "# Use your functions to show that 2 + 2 != 3 + 3 = 6\n", |
320 | | - "\n" |
| 354 | + "print(same(add(2, 2), add(3, 3)))\n" |
321 | 355 | ] |
322 | 356 | }, |
323 | 357 | { |
|
333 | 367 | }, |
334 | 368 | { |
335 | 369 | "cell_type": "code", |
336 | | - "execution_count": null, |
| 370 | + "execution_count": 25, |
337 | 371 | "metadata": {}, |
338 | | - "outputs": [], |
| 372 | + "outputs": [ |
| 373 | + { |
| 374 | + "name": "stdout", |
| 375 | + "output_type": "stream", |
| 376 | + "text": [ |
| 377 | + "Hello, Bob\n" |
| 378 | + ] |
| 379 | + } |
| 380 | + ], |
339 | 381 | "source": [ |
340 | 382 | "name = 'Bob'\n", |
341 | 383 | "\n", |
|
375 | 417 | }, |
376 | 418 | { |
377 | 419 | "cell_type": "code", |
378 | | - "execution_count": null, |
| 420 | + "execution_count": 26, |
379 | 421 | "metadata": {}, |
380 | | - "outputs": [], |
| 422 | + "outputs": [ |
| 423 | + { |
| 424 | + "ename": "ValueError", |
| 425 | + "evalue": "invalid literal for int() with base 10: 'This is not an integer'", |
| 426 | + "output_type": "error", |
| 427 | + "traceback": [ |
| 428 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 429 | + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", |
| 430 | + "Cell \u001b[0;32mIn[26], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mThis is not an integer\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", |
| 431 | + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'This is not an integer'" |
| 432 | + ] |
| 433 | + } |
| 434 | + ], |
381 | 435 | "source": [ |
382 | 436 | "int(\"This is not an integer\")" |
383 | 437 | ] |
|
391 | 445 | }, |
392 | 446 | { |
393 | 447 | "cell_type": "code", |
394 | | - "execution_count": null, |
| 448 | + "execution_count": 27, |
395 | 449 | "metadata": {}, |
396 | | - "outputs": [], |
| 450 | + "outputs": [ |
| 451 | + { |
| 452 | + "name": "stdout", |
| 453 | + "output_type": "stream", |
| 454 | + "text": [ |
| 455 | + "Converting 0 to an integer 0\n", |
| 456 | + "Converting 1 to an integer 1\n", |
| 457 | + "Converting 65 to an integer 65\n" |
| 458 | + ] |
| 459 | + }, |
| 460 | + { |
| 461 | + "ename": "ValueError", |
| 462 | + "evalue": "invalid literal for int() with base 10: 'Bob'", |
| 463 | + "output_type": "error", |
| 464 | + "traceback": [ |
| 465 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 466 | + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", |
| 467 | + "Cell \u001b[0;32mIn[27], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m e \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m65\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mBob\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m23\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlarry\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[0;32m----> 2\u001b[0m i \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43me\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConverting \u001b[39m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m to an integer \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", |
| 468 | + "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Bob'" |
| 469 | + ] |
| 470 | + } |
| 471 | + ], |
397 | 472 | "source": [ |
398 | 473 | "for e in [0,1, 65, 'Bob', 23,'larry']:\n", |
399 | 474 | " i = int(e)\n", |
|
409 | 484 | }, |
410 | 485 | { |
411 | 486 | "cell_type": "code", |
412 | | - "execution_count": null, |
| 487 | + "execution_count": 28, |
413 | 488 | "metadata": {}, |
414 | | - "outputs": [], |
| 489 | + "outputs": [ |
| 490 | + { |
| 491 | + "name": "stdout", |
| 492 | + "output_type": "stream", |
| 493 | + "text": [ |
| 494 | + "Converting 0 to an integer 0\n", |
| 495 | + "Converting 1 to an integer 1\n", |
| 496 | + "Converting 65 to an integer 65\n", |
| 497 | + "Could not convert Bob to an integer\n", |
| 498 | + "Converting 23 to an integer 23\n", |
| 499 | + "Could not convert larry to an integer\n" |
| 500 | + ] |
| 501 | + } |
| 502 | + ], |
415 | 503 | "source": [ |
416 | 504 | "for e in [0,1, 65, 'Bob', 23,'larry']:\n", |
417 | 505 | "\n", |
|
436 | 524 | }, |
437 | 525 | { |
438 | 526 | "cell_type": "code", |
439 | | - "execution_count": null, |
| 527 | + "execution_count": 33, |
440 | 528 | "metadata": {}, |
441 | | - "outputs": [], |
| 529 | + "outputs": [ |
| 530 | + { |
| 531 | + "ename": "ZeroDivisionError", |
| 532 | + "evalue": "division by zero", |
| 533 | + "output_type": "error", |
| 534 | + "traceback": [ |
| 535 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 536 | + "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", |
| 537 | + "Cell \u001b[0;32mIn[33], line 13\u001b[0m\n\u001b[1;32m 5\u001b[0m my_list \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m]\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m#print(my_list['Bob']) # TypeError: list indices must be integers or slices, not str\u001b[39;00m\n\u001b[1;32m 8\u001b[0m \n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m#print(my_list[20]) # IndexError: list index out of range\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m#assert False # AssertionError ( Although you don't usually catch assertions ) \u001b[39;00m\n\u001b[0;32m---> 13\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;241;43m10\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m \u001b[38;5;66;03m# ZeroDivisionError: division by zero\u001b[39;00m\n", |
| 538 | + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" |
| 539 | + ] |
| 540 | + } |
| 541 | + ], |
442 | 542 | "source": [ |
443 | 543 | "# Run me! Uncomment one of the code lines to see the error it produces\n", |
444 | 544 | "# Put the comment back to see another error\n", |
|
450 | 550 | "\n", |
451 | 551 | "#print(my_list[20]) # IndexError: list index out of range\n", |
452 | 552 | "\n", |
453 | | - "# assert False # AssertionError ( Although you don't usually catch assertions ) \n", |
| 553 | + "#assert False # AssertionError ( Although you don't usually catch assertions ) \n", |
454 | 554 | "\n", |
455 | | - "# x = 10 / 0 # ZeroDivisionError: division by zero\n", |
| 555 | + "x = 10 / 0 # ZeroDivisionError: division by zero\n", |
456 | 556 | "\n" |
457 | 557 | ] |
458 | 558 | }, |
|
466 | 566 | }, |
467 | 567 | { |
468 | 568 | "cell_type": "code", |
469 | | - "execution_count": null, |
| 569 | + "execution_count": 36, |
470 | 570 | "metadata": {}, |
471 | | - "outputs": [], |
| 571 | + "outputs": [ |
| 572 | + { |
| 573 | + "name": "stdout", |
| 574 | + "output_type": "stream", |
| 575 | + "text": [ |
| 576 | + "Got a zero division error division by zero\n", |
| 577 | + "Got an exception division by zero\n" |
| 578 | + ] |
| 579 | + } |
| 580 | + ], |
472 | 581 | "source": [ |
473 | 582 | "\n", |
474 | 583 | "# You can list the different types of different `except` clauses:\n", |
475 | 584 | "\n", |
476 | 585 | "try:\n", |
477 | | - " pass# do something\n", |
| 586 | + " i = 10/0\n", |
478 | 587 | "except ValueError as e:\n", |
479 | 588 | " print(f\"Got a value error {e}\")\n", |
480 | 589 | "except TypeError as e:\n", |
|
487 | 596 | "# But there are a lot of reasons to *not* do this\n", |
488 | 597 | "\n", |
489 | 598 | "try:\n", |
490 | | - " pass# do something\n", |
| 599 | + " i = 10/0\n", |
491 | 600 | "except Exception as e:\n", |
492 | 601 | " # Get all types of exceptions. \n", |
493 | 602 | " print(f\"Got an exception {e}\")" |
|
517 | 626 | "\n", |
518 | 627 | "if 5 in [1,2,3,4,5]:\n", |
519 | 628 | " print(\"It's In!\")\n", |
| 629 | + "while True:\n", |
| 630 | + " n = input(\"Give me a string: \")\n", |
| 631 | + " if n != 'q':\n", |
| 632 | + " try:\n", |
| 633 | + " int(n)\n", |
| 634 | + " except n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n", |
| 635 | + " print(\"You got one!\")\n", |
| 636 | + " else:\n", |
| 637 | + " break\n", |
| 638 | + "\n", |
| 639 | + "\n", |
| 640 | + "\n", |
520 | 641 | " \n", |
521 | 642 | "\n", |
522 | 643 | "\n" |
523 | 644 | ] |
524 | 645 | }, |
525 | 646 | { |
526 | 647 | "cell_type": "code", |
527 | | - "execution_count": null, |
| 648 | + "execution_count": 4, |
528 | 649 | "metadata": {}, |
529 | 650 | "outputs": [], |
530 | 651 | "source": [ |
|
0 commit comments