{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Debugging practice\n", "\n", "Debugging your code is an inescapable part of coding just like revising your writing is an inescapable part of writing. These exercises are designd to help you practice your debugging skills while demonstrating a few common pitfalls.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. This bit of code is supposed to iterate through a shuffled list. Why is it not working?" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'NoneType' object is not iterable", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb Cell 3\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mrandom\u001b[39;00m\n\u001b[1;32m 2\u001b[0m vowels \u001b[39m=\u001b[39m random\u001b[39m.\u001b[39mshuffle([\u001b[39m'\u001b[39m\u001b[39ma\u001b[39m\u001b[39m'\u001b[39m,\u001b[39m'\u001b[39m\u001b[39me\u001b[39m\u001b[39m'\u001b[39m,\u001b[39m'\u001b[39m\u001b[39mi\u001b[39m\u001b[39m'\u001b[39m,\u001b[39m'\u001b[39m\u001b[39mo\u001b[39m\u001b[39m'\u001b[39m,\u001b[39m'\u001b[39m\u001b[39mu\u001b[39m\u001b[39m'\u001b[39m])\n\u001b[0;32m----> 4\u001b[0m \u001b[39mfor\u001b[39;00m cur_vowel \u001b[39min\u001b[39;00m vowels:\n\u001b[1;32m 5\u001b[0m \t\u001b[39mprint\u001b[39m(cur_vowel)\n", "\u001b[0;31mTypeError\u001b[0m: 'NoneType' object is not iterable" ] } ], "source": [ "import random\n", "vowels = random.shuffle(['a','e','i','o','u'])\n", "\n", "for cur_vowel in vowels:\n", "\tprint(cur_vowel)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. `Sets` are like lists except they allow us to perform set operations like union, intersection, difference etc. Unlike lists which have no restrictions on duplicated elements, a set -- by definition -- only contains unique elements. Because sets are implemented in a very efficient way, we can use them to remove duplicates from a list. For example:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['dog', 'cat', 'wolf', 'dog', 'dog']\n", "['cat', 'wolf', 'dog']\n" ] } ], "source": [ "list_with_duplicates = ['dog', 'cat', 'wolf', 'dog', 'dog']\n", "print(list_with_duplicates)\n", "print(list(set(list_with_duplicates))) #voila, duplicates gone!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I'm trying to do something similar here, but running into a problem. Help!" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['3', 3, 'guitar', 'violin']\n" ] } ], "source": [ "duplicated_list = (violin, 'guitar', 'guitar', '3',3)\n", "print(list(set(duplicated_list))) #voila, duplicates gone?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. I'm trying to iterate through a list, check if each element is a palindrome (reads the same backwards and forwards), and remove all the non-palindromes so that so that at the end I only have palindromes. The palindrome checker is working... but something else is not. Fix it! Also what is ths .replace(' ','') doing?" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb Cell 9\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[39mfor\u001b[39;00m i,j \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(list_of_strings):\n\u001b[1;32m 8\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m is_palindrome(j\u001b[39m.\u001b[39mreplace(\u001b[39m'\u001b[39m\u001b[39m \u001b[39m\u001b[39m'\u001b[39m,\u001b[39m'\u001b[39m\u001b[39m'\u001b[39m)):\n\u001b[0;32m----> 9\u001b[0m list_of_strings\u001b[39m.\u001b[39;49mremove(i)\n\u001b[1;32m 10\u001b[0m \u001b[39mprint\u001b[39m(list_of_strings)\n", "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "def is_palindrome(str):\n", " str = str.replace(' ','') # remove spaces\n", " return str == str[::-1]\n", "\n", "list_of_strings = ['dog', 'rotator', 'dod', 'cat', 'kayak', 'malyalam', 'a man a plan a canal panama']\n", "\n", "for i,j in enumerate(list_of_strings):\n", " if not is_palindrome(j.replace(' ','')):\n", " list_of_strings.remove(i)\n", "print(list_of_strings)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. I'm trying to iterate through a list, but something is wrong. What happened?\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] }, { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb Cell 11\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m listSize \u001b[39m=\u001b[39m \u001b[39mlen\u001b[39m(myList)\n\u001b[1;32m 6\u001b[0m \u001b[39mfor\u001b[39;00m index \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(listSize\u001b[39m+\u001b[39m\u001b[39m1\u001b[39m):\n\u001b[0;32m----> 7\u001b[0m \u001b[39mprint\u001b[39m(myList[index])\n", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "myList = []\n", "for i in range(10):\n", " myList.append(i)\n", "\n", "listSize = len(myList)\n", "for index in range(listSize+1):\n", " print(myList[index])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Now instead of a list, I'm trying to iterate through a dictionary and print out just the car *models* stored inside it. (Stelvio, M3, etc.) Why isn't it working?" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "unmatched ')' (3056340552.py, line 9)", "output_type": "error", "traceback": [ "\u001b[0;36m Input \u001b[0;32mIn [16]\u001b[0;36m\u001b[0m\n\u001b[0;31m for key in thisDict):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unmatched ')'\n" ] } ], "source": [ "thisDict = {\n", " 'brand':['Alfa Romeo','BMW','Chevrolet','Dodge'],\n", " 'year':[2018,2023,1997,1969],\n", " 'model':['Stelvio','M3','Corvette','Challenger'],\n", " 'color':['black','green','red','black'],\n", " 'horsepower':[280,473,345,425]\n", "}\n", "\n", "for key in thisDict):\n", " print(thisDict[key])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. The function I defined is supposed to compute the factorial of the number (y!), but it's not working. " ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "def compute_factorial(y):\n", " for i in range(y,-1):\n", " y*=i\n", " return y\n", "\n", "print(compute_factorial(5)) #should return 120 (5*4*3*2*1)... \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. I'm trying to create a function that multiplies the number I input by 3, but why do I get `None`?" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "def multiply_by_three(x):\n", " x*=3\n", "\n", "a = 5\n", "x = multiply_by_three(a)\n", "print(x)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.8.13 ('psych750')", "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.13" }, "toc": { "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "toc_cell": false, "toc_position": { "height": "526px", "left": "0px", "right": "1095px", "top": "111px", "width": "185px" }, "toc_section_display": "block", "toc_window_display": true }, "vscode": { "interpreter": { "hash": "57beecaf6908bae4f97de5e2dc8e8d0311fae5bc989593c172c307d13e31f6e4" } } }, "nbformat": 4, "nbformat_minor": 2 }