Identifying and Correcting Errors
Practice with identifying and correcting code blocks
Here are some code segments you can practice fixing:
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) + "st 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?
- 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)
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 = 0
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)
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)
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?
- Make changes to get the intended outcome.
numbers = []
newNumbers = []
i = 0
while i < 101:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] == 0:
continue
elif 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?
- 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.0
current_order = []
#shows the user the menu and prompts them to select an item
# multiple item code
name = input("What is your name?")
print("Welcome " , name, "!")
print("Here is our menu:")
for k,v in menu.items():
print(k + " $" + str(v))
item = input("Please select an item from the menu")
item = item.lower()
done = False
while (done == False):
if item in menu.keys():
quantity = float(input("How many do you want?"))
current_order.append(str(item) + " X " + str(quantity))
total += (menu[item] * quantity)
print(str(int(quantity)) + " " + item + "(s)" + " added")
else:
if (item == "done"):
done = True
break
else:
print("Please check the menu and enter a correct item, or 'done' if you are finished with your order.")
item = input("What else do you want today? Type 'done' to get your final total.")
item = item.lower()
print("Your order:")
print(current_order)
print("Price:")
print("$" + str(round(total,2)))
Hacks
- What errors may arise in your project?
In program development, many small errors occur. These small errors include an incorrect usage of syntax, a typo, or a missed character. Most errors are like this but sometimes the error is bigger. Entire procedures can have the wrong purpose, code is typed using an incorrect language, program is set up incorrectly, and code could be blocked to some members of the team. All of these errors can break the entire code so all errors have to be fixed before we push our code.
- What are some test cases that can be used?
Proper inputs for our team's program would be english words but we would have to test more than just that. Test cases that can be used would include any word from any language with any amount of letters as well as numbers or other special characters. This would mostly likely break the code and that is why my group must test them. It would be the best way to fix them, as we can debug and fgure out how to not let this break the code.