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:

#Create a list of the alphabet

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



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 b is the 2 letter in the alphabet

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



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 b is the 2 letter in the alphabet

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


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.


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


numbers = []
newNumbers = []
i = 0

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

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

print(newNumbers) 


[0, 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]

Challenge

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

Hint:

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

#Define dictionary containing all menu items and their corresponding cost 
#All costs are pre-Covid because no way we're getting prices this low anymore

menu =  {"burger": 13.99,
         "wings": 9.50,
         "fries": 5.50,
         "chips": 1.99,
         "soda": 2.99,
         "cookie": 1.99,
         "salad": 9.99}

#Sets the total cost of cart to 0 dollars
total = 0

#Shows the user all menu items and prompts them to select an item
print("Menu:")
print("-------------------------------")
for k,v in menu.items():
    print(k + "  $" + str(v))


#Defines variable "yn" to be used as the condition in the while loop
yn = "y"

#Check if yes/no or "yn" equals y meaning yes
#if yes, continue or redo the loop
while yn == "y":
    print("------------------------------------")

    #Ask the user which menu item they would like
    item = input("Please select an item from the menu")

    #Display the cost of the desired item
    print("cost of: " + 
          item)
    print("$" + str(menu[item]))

    #Add the cost of the desired menu item to the total bill for the day
    total += menu[item]

    #Ask valued customer once again whether they would like to buy anything else
    yn = input("Would you like to buy anything else? (y/n)")

#Print cost of items without tax
print("Cost of items: " + str(total))

#Print total cost of items including tax
print("Tax: " + str(round(0.0725*total, 2)))

#Prompt user, asking for tip percentage for our amazing employees because they are so great
tip = input("Enter tip amount here: ")

#Make sure there aren't any percent symbols before adding to total
tip = tip.replace('%', '')

#Multiply percentage tip to find how much the stingy customer is tipping
tipval = (float(tip)*.01)*total

#Print how much the tip comes out to be
print("Tip: " + str(round(float(tipval), 2)) + " (" + tip + "%)")

#Print the total cost of the customer's amazing meal and experience
print("Total: " + str(round(1.0725*total, 2) + round(float(tipval), 2)))

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.