Big Idea 1 'Identifying and Correct Errors'
Practice with identifying and correcting code blocks
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?
- 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
My name is Edwin Abraham, and I decided to input my last name initial, so when I inputted 'a', I got 1 which is true because it is the first key in the list.
The problem here was the the computer starts from 0 but we want it to start at 1, so we had to change i += 0 to 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?
- Make changes to get the intended outcome.
letter = input("What letter would you like to check?")
for i in alphabetList:
count = 0
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)
evens = []
i = 0
while i <= 10:
evens.append(i)
i += 2
print(evens)
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []
for i in numbers:
if (numbers[i] % 2 == 0): # returns the remainder
evens.append(numbers[i])
print(evens)
numbers = [0,1,2,3,4,5,6,7,8,9,10]
odds = []
for i in numbers:
if (numbers[i] % 2 == 1): # returns the remainder
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?
- 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:
continue # you can also put pass and then make the next function elif
if numbers[i] % 5 == 0:
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 0: # adding el in front of the if makes it an else function
newNumbers.append(numbers[i])
print(newNumbers)
numbers = []
newNumbers = []
i = 0
while i <= 100:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] == 0:
continue # you can also put pass and then make the next function elif
if numbers[i] % 5 == 1:
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 1: # adding el in front of the if makes it an else function
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?
- 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 # variable is being assigned
#shows the user the menu and prompts them to select an item
print("This is the menu, what would you like to order?")
print("To stop ordering please type stop")
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
while True:
item = input("Please select an item from the menu")
if item == "burger":
total += 3.99
print(total)
elif item == "fries":
total += 1.99
print(total)
elif item == "drink": # == means that you are comparing the variable, don't assign variable
total += 0.99
print(total)
elif item == "stop":
print("Thank You for Ordering, Here is your total:", total)
break
else:
print("That is not on the menu")
exit(0)
#code should add the price of the menu items selected by the user
#print(total)
Solving Hacks
Now is a good time to think about Testing of your teams final project...
- What errors may arise in your project?
- I did not know how to use the string
- When I typed stop the code always broke
- When I typed burger it did not work and ran the else statement
- The break crashed the system
- What are some test cases that can be used?
- inputting burger and getting the price of burger
- adding items to the list and seeing if the total is right
- Putting stop to see if the program outputs the right items
- Make sure to document any bugs you encounter and how you solved the problem.
- The code was always running the else part so I had to fix my indentation
- Whenever I typed something on the menu, it would run the else statement, so I had to add elif to the functions above
- 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.