Big Idea 1 'Identifying and Correcting Errors' Hacks
Practice with identifying and correcting code blocks & Hacks
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabetList = []
for i in alphabet:
alphabetList.append(i)
print(alphabetList)
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?
- A or C because it is easy for the user to correctly identify their position.
- 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 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?
- A, B, or Z because their position is commonly known.
- 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
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)
- Change initial value of i to an odd number
evens = []
i = 1
while i <= 10:
evens.append(i)
i += 2
print(evens)
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)
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)
- change remainder to 1
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []
for i in numbers:
if (numbers[i] % 2 == 1):
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]
odds = []
for i in numbers:
if (numbers[i] % 2 == 1):
odds.append(numbers[i])
print(odds)
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?
- Every multiple of 10 is duplicated in the place of the next value.
- Make changes to get the intended outcome.
numbers = []
newNumbers = []
i = 0
while i <= 100:
numbers.append(i)
i += 1
for i in range(1, 101):
if numbers[i] % 5 == 0:
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 0:
newNumbers.append(numbers[i])
print(newNumbers)
Challenge
This code segment is at a very early stage of implementation.
- What are some ways to (user) error proof this code?
- Execute the code and work through any bugs
- 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 = {"burger": 3.99,
"fries": 1.99,
"drink": 0.99}
total = 0
#shows the user the menu and prompts them to select an item
print("Menu")
for k,v in menu.items():
print(k + " $" + str(v)) #why does v have "str" in front of it?
#ideally the code should prompt the user multiple times
item = input("Please select an item from the menu, or click enter if you are done.")
full = 0
j = []
while item != "":
j.append(item)
item = input("Please select an item from the menu, or click enter if you are done.")
for item in j:
if item in menu.keys():
full = full + menu[item]
#code should add the price of the menu items selected by the user
print(round(full, 3))
AP Classroom Video Notes
- For errors in an algorithm or program:
- Identify the error
- Logic error
- mistake in algorithm or program that causes it to behave incorrectly or unexpectedly
- Syntax error
- A mistake where the rules of the programming language are not followed
- run-time error
- Occurs during execution of program
- Programming languages define their own runtime errors
- Overflow error
- Error that occurs when a computer attempts to handle a number that is outside of the defined range of values
- Logic error
- Correct errors
- test cases
- testing certain inputs to figure out what is wrong with the program
- Border cases
- testing inputs that are likely to mess up in the program
- hand tracing
- Writing out the values of the variable within the loop as it iterates to determine if the outcome is correct
- Really only useful for small code segments that iterate a small number of times
- visualizations
- debuggers
- adding extra output statement(s)
- Helps programmers determine where an error is in a program
- test cases
- Identify the error
- Programmers constantly run into errors when writing programs
- They expect to encounter errors and are prepared to resolve them
- Testing
- uses defined inputs to ensure that an algorithm or program is producing the expected outcome(s)
- Programmers use the results from testing to revise their algorithms or programs
- Defined inputs should demonstrate the different expected outcomes that are at or just beyond the extremes of input data
- Program requirements are needed to identify appropriate defined inputs for testing
- uses defined inputs to ensure that an algorithm or program is producing the expected outcome(s)
Hacks
Now is a good time to think about Testing of your teams final project...
- What errors may arise in your project?
- misdirection/confusion of pages
- What are some test cases that can be used?
- Corner cases
- Make sure to document any bugs you encounter and how you solved the problem.
- Okay.
- 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.