Big Idea 1 'Identifying and Correcting 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
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+1) + " 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 = 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)
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []
for i in numbers:
if (numbers[i] % 2 == 1): # % sign is a division sign
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 == 0):
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.
# old version with repeating multiples of 10
numbers = []
newNumbers = []
i = 0
while i < 100:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] % 5 == 0: # if it is divisible by 5, it will append the list
newNumbers.append(numbers[i])
if numbers[i] % 2 == 0: # or else if it is divisible by 2, it will also append the list
newNumbers.append(numbers[i])
print(newNumbers)
# new version with no repeats for multiples of 10
numbers = []
newNumbers = []
i = 0
while i < 100:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] % 5 == 0: # if it is divisible by 5, it will append the list
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 0: # or else if it is divisible by 2, it will also append the list
newNumbers.append(numbers[i])
print(newNumbers)
# new version without 0 in the list
# 0 is involved in the list "numbers", so we need to make the code omit the 0
numbers = []
newNumbers = []
i = 0
while i < 100:
numbers.append(i)
i += 1
# using del list[0] to perform removal
for i in numbers:
if numbers[i] % 5 == 0: # if it is divisible by 5, it will append the list
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 0: # or else if it is divisible by 2, it will also append the list
newNumbers.append(numbers[i])
del newNumbers[0] # deletes 0 from the list to be printed
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
#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. Once you are done selecting items, type 'done'")
# create an if loop
if item.lower() == "burger": # .lower() allows the same output whether the person types upper or lowercase
print("1 burger: $3.99")
total = total + 3.99
elif item.lower() == "fries": # create an if loop for fries
print("1 fries: $1.99")
total = total + 1.99
elif item.lower() == "drink": # create an if loop for drinks
print("1 drink: $0.99")
total = total + 0.99
elif input:
print("You misspelled that")
#code should add the price of the menu items selected by the user
print("Your total is $" + str(total))
menu = {"burger": 3.99,
"fries": 1.99,
"drink": 0.99} # use of dictionary
total = 0
#shows the user the menu and prompts them to select an item
print("Shreya's Restaurant Menu")
for k,v in menu.items(): # "k" is the item name, "v" is the price. "k" stands for key and "v" stands for value.
print(k + " $" + str(v)) #why does v have "str" in front of it?
#ideally the code should prompt the user multiple times
print("Please select items from the menu. Once you are done selecting items, type 'done'")
item = input("Please select items from the menu. Once you are done selecting items, type 'done'")
while True: # Loop continuously
inp = input() # Get the input
if inp == "burger": # input for burger
print("1 burger: $3.99")
total = total + 3.99
elif inp == "fries": # input for fries
print("1 fries: $1.99")
total = total + 1.99
elif inp == "drink": # input for a drink
print("1 drink: $0.99")
total = total + 0.99
elif inp == "done": # input for completing the order
break # stop asking the user for inputs
# print the total order cost
print("Your total is $" + str(total))
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.