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

  • What is a good test case to check the current outcome? Why?
  • Make changes to get the intended outcome.
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
The letter a is the 1 letter in the alphabet

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

  • What is a good test case to check the current outcome? Why?
  • Make changes to get the intended outcome.
letter = input("What letter would you like to check?")
count = 0
for i in alphabetList:
    if i == letter:
        print("The letter " + letter + " is the " + str(count+1) + " letter in the alphabet")
    count += 1
The letter a is the 1 letter in the alphabet

This code outputs the even numbers from 0 - 10 using a while loop.

  • Analyze this code to determine what can be changed to get the outcome to be odd numbers. (Code block below)
evens = []
i = 0

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

print(evens)    
[0, 2, 4, 6, 8, 10]

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

odds = []
i = 0

while i <= 9:
    odds.append(i+1)
    i += 2

print(odds)
[1, 3, 5, 7, 9]

This code outputs the even numbers from 0 - 10 using a for loop.

  • Analyze this code to determine what can be changed to get the outcome to be odd numbers. (Code block below)
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)
[0, 2, 4, 6, 8, 10]

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]
odds = []

for i in numbers:
    if (numbers[i] % 2 == 1):
        odds.append(numbers[i])

print(odds)
[1, 3, 5, 7, 9]

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

  • What values are outputted incorrectly. Why?
  • Make changes to get the intended outcome.
numbers = []
newNumbers = []
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 == 1:
        newNumbers.append(numbers[i])

print(newNumbers) 
[0, 1, 3, 5, 5, 7, 9, 10, 11, 13, 15, 15, 17, 19, 20, 21, 23, 25, 25, 27, 29, 30, 31, 33, 35, 35, 37, 39, 40, 41, 43, 45, 45, 47, 49, 50, 51, 53, 55, 55, 57, 59, 60, 61, 63, 65, 65, 67, 69, 70, 71, 73, 75, 75, 77, 79, 80, 81, 83, 85, 85, 87, 89, 90, 91, 93, 95, 95, 97, 99]

Challenge

This code segment is at a very early stage of implementation.

  • What are some ways to (user) error proof this code?
  • The code should be able to calculate the cost of the meal of the user

Hint:

  • write a “single” test describing an expectation of the program of the program
  • test - input burger, expect output of burger price
  • run the test, which should fail because the program lacks that feature
  • write “just enough” code, the simplest possible, to make the test pass

Then repeat this process until you get program working like you want it to work.

menu =  {"tacos": 9.99,
         "fries": 5.99,
         "drink": 4.99}
print (menu)

order= input("Hi, What would you like to eat? >>")

if order == "tacos":
    print("you ordered tacos, your total is $5.99")
elif order == "fries":
    print("you ordered fries, your total is $9.99")
elif order == "drink":
    print("you ordered a drink, your total is $4.99")
elif order != "tacos" or "fries" or "drink":
    print("That item is not on the menu, try re-order")
{'tacos': 9.99, 'fries': 5.99, 'drink': 4.99}
you ordered tacos, your total is $5.99

Hacks

  • What errors may arise in your project?

    • Some error that may arise in our project is dictionary errors, api errors, and syntax errors. When they input a letter it may not be read correctly and place the letter in the wrong spot or it may not say that a correct letter is included in a word.
  • What are some test cases that can be used?

    • We can test inputting some correct letters and some wrong letters. This will lead to our desired output. If we type in a wrong letter a body part will appear.
  • Make sure to document any bugs you encounter and how you solved the problem.

    • I will document any errors I have and inform my team.
  • What are “single” tests that you will perform on your project? Or, your part of the project?

    • I will first play the game for myself and then I will ask peers to review the game aswell.