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 h is the 8 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 = 1

for i in alphabetList:
    if i == letter:
        print("The letter " + letter + " is the " + str(count) + " letter in the alphabet")
    count += 1
The letter z is the 26 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 = 1

while i <= 10:
    odds.append(i)
    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): # % is modulus that returns remainder
        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] == 0:
        pass   # if it's zero, do nothing, you can also use continue, if it's equal to zero, skip to loop and bypass the code and do the same thing
    elif numbers[i] % 5 == 0:
        newNumbers.append(numbers[i])
    elif numbers[i] % 2 == 0:   # if it doesn't do first one, it does this, otherwise, nothing happens
        newNumbers.append(numbers[i])

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

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.

my_Menu =  {"pasta": 7.08,
            "pizza": 11.53,
            "breadstick": 0.99,
            "drink": 1.09}  # define my menu as a dictionary data type, key is the name of food, value is the price 

price_total = 0  #set initial value, default value means function argument will take this value if no argument value is passed during function call

print("======================") #print menu decoration line on top
print("~~~ Kaylee's Menu ~~~")
# print a nice menu that allows user to choose from 
for x,y in my_Menu.items():
    print(x,": ","$",y)
print("======================") #print menu decoration line at bottom

print("Welcome to Kaylee's Italian Food Truck, what would you like to order from our menu?")
myUserInput = "pasta"  #define and set initial value
myUserInput = input() #define variable to hold user input
myUserOrderKey = myUserInput  #define and set initial value-- used to print out user's selected key and value
myCounter = 0 #count through the loop 
myTotalOrderQuestion = "How many " + myUserInput + "s would you like to order?"
myError = "Sorry, we don't have '" + myUserInput + "' in our menu"
totalUserInput = 0 #define variable


for x,y in my_Menu.items(): #loop through my menu
    if x == myUserInput: #if the user input matches a key in my menu4
        print("Your order is: ",x,y) #then print the key and value 
        break #stop looping when you get result 
    else:
       myCounter += 1 
       if myCounter == 4: # if loop four times, it means we don't have the item in the menu 
            print(myError)
if myCounter != 4:   
    print(myTotalOrderQuestion) 
    totalUserInput = int(input())   #only get user input when you need it, otherwise it will always prompt
    myUserTotal = "Your total for " + str(totalUserInput) + " "+ x +"(s) is $" + str(y*totalUserInput)+ ". Enjoy!"
    if totalUserInput >= 1:
        print(myUserTotal)
    else:
        print("Please give a number of at least one.")
    
======================
~~~ Kaylee's Menu ~~~
pasta :  $ 7.08
pizza :  $ 11.53
breadstick :  $ 0.99
drink :  $ 1.09
======================
Welcome to Kaylee's Italian Food Truck, what would you like to order from our menu?
Your order is:  pasta 7.08
How many pastas would you like to order?
Your total for 2 pasta(s) is $14.16. Enjoy!

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.
  • What are “single” tests that you will perform on your project? Or, your part of the project?
    • As Hack Design and Test plan action … Divide these “single” tests into Issues for Scrum Board prior to coding. FYI, related tests could be in same Issue by using markdown checkboxes to separate tests.