Debugging practice#

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.

  1. This bit of code is supposed to iterate through a shuffled list. Why is it not working?

import random
vowels = random.shuffle(['a','e','i','o','u'])

for cur_vowel in vowels:
	print(cur_vowel)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb Cell 3 in <cell line: 4>()
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#W2sZmlsZQ%3D%3D?line=0'>1</a> import random
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#W2sZmlsZQ%3D%3D?line=1'>2</a> vowels = random.shuffle(['a','e','i','o','u'])
----> <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#W2sZmlsZQ%3D%3D?line=3'>4</a> for cur_vowel in vowels:
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#W2sZmlsZQ%3D%3D?line=4'>5</a> 	print(cur_vowel)

TypeError: 'NoneType' object is not iterable
  1. 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:

list_with_duplicates = ['dog', 'cat', 'wolf', 'dog', 'dog']
print(list_with_duplicates)
print(list(set(list_with_duplicates))) #voila, duplicates gone!
['dog', 'cat', 'wolf', 'dog', 'dog']
['cat', 'wolf', 'dog']

I’m trying to do something similar here, but running into a problem. Help!

duplicated_list = (violin, 'guitar', 'guitar', '3',3)
print(list(set(duplicated_list))) #voila, duplicates gone?
['3', 3, 'guitar', 'violin']
  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?

def is_palindrome(str):
    str = str.replace(' ','') # remove spaces
    return str == str[::-1]

list_of_strings = ['dog', 'rotator', 'dod', 'cat', 'kayak', 'malyalam', 'a man a plan a canal panama']

for i,j in enumerate(list_of_strings):
    if not is_palindrome(j.replace(' ','')):
        list_of_strings.remove(i)
print(list_of_strings)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb Cell 9 in <cell line: 7>()
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X11sZmlsZQ%3D%3D?line=6'>7</a> for i,j in enumerate(list_of_strings):
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X11sZmlsZQ%3D%3D?line=7'>8</a>     if not is_palindrome(j.replace(' ','')):
----> <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X11sZmlsZQ%3D%3D?line=8'>9</a>         list_of_strings.remove(i)
     <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X11sZmlsZQ%3D%3D?line=9'>10</a> print(list_of_strings)

ValueError: list.remove(x): x not in list
  1. I’m trying to iterate through a list, but something is wrong. What happened?

myList = []
for i in range(10):
    myList.append(i)

listSize = len(myList)
for index in range(listSize+1):
    print(myList[index])
0
1
2
3
4
5
6
7
8
9
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb Cell 11 in <cell line: 6>()
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X13sZmlsZQ%3D%3D?line=4'>5</a> listSize = len(myList)
      <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X13sZmlsZQ%3D%3D?line=5'>6</a> for index in range(listSize+1):
----> <a href='vscode-notebook-cell:/Users/glupyan/gitRepos/psych750.github.io/notebooks/activity_debugging.ipynb#X13sZmlsZQ%3D%3D?line=6'>7</a>     print(myList[index])

IndexError: list index out of range
  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?

thisDict = {
    'brand':['Alfa Romeo','BMW','Chevrolet','Dodge'],
    'year':[2018,2023,1997,1969],
    'model':['Stelvio','M3','Corvette','Challenger'],
    'color':['black','green','red','black'],
    'horsepower':[280,473,345,425]
}

for key in thisDict):
    print(thisDict[key])
  Input In [16]
    for key in thisDict):
                       ^
SyntaxError: unmatched ')'
  1. The function I defined is supposed to compute the factorial of the number (y!), but it’s not working.

def compute_factorial(y):
    for i in range(y,-1):
        y*=i
    return y

print(compute_factorial(5)) #should return 120 (5*4*3*2*1)... 
5
  1. I’m trying to create a function that multiplies the number I input by 3, but why do I get None?

def multiply_by_three(x):
    x*=3

a = 5
x = multiply_by_three(a)
print(x)
None