College Board Big Idea 1

Identifying and Correcting Errors (Unit 1.4)

Become familiar with types of errors and strategies to fixing them

  • Lightly Review Videos and take notes on topics with Blog
  • Complete assigned MCQ questions

Here are some code segments you can practice fixing:

alphabet = "abcdefghijklmnopqrstuvwxyz"

alphabetList = []

for i in alphabet:
    alphabetList.append(i)

print(alphabetList)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

The intended outcome is to determine where the letter is in the alphabet using a while loop

  • There are two changes you can make
letter = input("What letter would you like to check?")

i = 0

while i < 26:
    if alphabetList[i] == letter:
        print("The letter " + letter + " is the " + str(i) + " letter in the alphabet")
    i += 1

The intended outcome is to determine where the letter is in the alphabet using a for loop

  • There are two changes you can make
letter = input("What letter would you like to check?")

i = 0

while i < 26:
    if alphabetList[i] == letter:
        print("The letter " + letter + " is the " + str(i + 1) + " letter in the alphabet")
    i += 1
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb Cell 8 in <cell line: 5>()
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X10sZmlsZQ%3D%3D?line=2'>3</a> i = 0
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X10sZmlsZQ%3D%3D?line=4'>5</a> while i < 26:
----> <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X10sZmlsZQ%3D%3D?line=5'>6</a>     if alphabetList[i] == letter:
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X10sZmlsZQ%3D%3D?line=6'>7</a>         print("The letter " + letter + " is the " + str(i + 1) + " letter in the alphabet")
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X10sZmlsZQ%3D%3D?line=7'>8</a>     i += 1

NameError: name 'alphabetList' is not defined

This code should output the evens numbers from 0 - 10 using a while loop.

  • How can you change the code to only print the odd numbers in the list?
evens = []
i = 0

while i <= 10:
    evens.append(numbers[i])
    i += 2

print(evens)    
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb Cell 10 in <cell line: 4>()
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X12sZmlsZQ%3D%3D?line=1'>2</a> i = 0
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X12sZmlsZQ%3D%3D?line=3'>4</a> while i <= 10:
----> <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X12sZmlsZQ%3D%3D?line=4'>5</a>     evens.append(numbers[i])
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X12sZmlsZQ%3D%3D?line=5'>6</a>     i += 2
      <a href='vscode-notebook-cell:/Users/chrisalbertson/vscode/APCSP/_notebooks/2022-10-03-AP-error_testing.ipynb#X12sZmlsZQ%3D%3D?line=7'>8</a> print(evens)

NameError: name 'numbers' is not defined

This code should output the odd numbers from 0 - 10 using a while loop.

evens = []
i = 0

while i <= 10:
    evens.append(numbers[i])
    i += 2

print(evens)

This code should output the evens numbers from 0 - 10 using a for loop.

  • How can you change the code to only print the odd numbers in the list?
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []

for i in numbers:
    if (numbers[i] % 2 == 0):
        evens.append(numbers[i])

print(evens)

This code should output the odd numbers from 0 - 10 using a for loop.

numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []

for i in numbers:
    if (numbers[i] % 2 == 0):
        evens.append(numbers[i])

print(evens)

The intended outcome is printing a number between 1 and 100 once, if it is a multiple of 2 or 5

  • 3 changes to the code will give the expected outcome.
numbers = [1]
newNumbers = [100]
i = 0

while i < 100:
    numbers.append(i)
    i += 1

for i in numbers:
    if numbers[i] % 5 == 0:
        newNumbers.append(numbers[i])
    if numbers[i] % 2 == 0:
        newNumbers.append(numbers[i])

print(newNumbers) 
[100, 0, 0, 0, 0, 2, 4, 5, 6, 8, 10, 10, 12, 14, 15, 16, 18, 20, 20, 22, 24, 25, 26, 28, 30, 30, 32, 34, 35, 36, 38, 40, 40, 42, 44, 45, 46, 48, 50, 50, 52, 54, 55, 56, 58, 60, 60, 62, 64, 65, 66, 68, 70, 70, 72, 74, 75, 76, 78, 80, 80, 82, 84, 85, 86, 88, 90, 90, 92, 94, 95, 96, 98]

Hacks

Now is a good time to think about Testing of your teams final project...

  • What errors may arise in your project?
  • What are some test cases that can be used?
  • Make sure to document any bugs you encounter and how you solved the problem.