Identifying and Correcting Errors
Practice with identifying and correcting code blocks
- Alphabet List:
- Alphabet Numbering: While Loop
- Alphabet Numbering System: Count
- Number List: While Loop
- Number List: For Loop
- Printing Numbers That Are Multiples of 2 or 5:
- Challenge
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabetList = []
for i in alphabet:
alphabetList.append(i)
print(alphabetList)
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") #adds 1 to the number that is counted by the program to make the number outputted true
i += 1
letter = input("What letter would you like to check?")
for i in alphabetList:
count = 1 # counting is started at 1 instead of 0
if i == letter:
print("The letter " + letter + " is the " + str(count) + " letter in the alphabet")
count += 1
evens = []
i = 0
while i <= 10:
evens.append(i)
i += 2
print(evens)
Outputting odd numbers:
odds = []
i = 0
while i <= 10:
odds.append(i + 1) # starts at 1 instead of 0 to give odd numbers
i += 2
print(odds)
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)
Outputting odd numbers:
numbers = [0,1,2,3,4,5,6,7,8,9,10]
odds = []
for i in numbers:
if (numbers[i] % 2 == 1): # start at 1 instead of 0 in order to get odd numbers
odds.append(numbers[i])
print(odds)
numbers = []
newNumbers = []
i = 0
while i < 100:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] == 0: # skips the number 0, so that the list is from 1 to 100
pass
elif numbers[i] % 5 == 0:
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 0: # elif makes the code not repeat numbers
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,
"salad": 4.99,
"toast": 2.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
while total < 3:
item = input("Please select 3 items from the menu")
print("Item ordered:", item.lower())
for k,v in menu.items():
if item.lower() == k:
print("Your total order is $", menu[item.lower()])
total = (total + 1)
break
if item.lower() != k:
print("Try choosing your item again.")
continue
#code should add the price of the menu items selected by the user
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.