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) + " 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 = 1
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)
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 == 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] % 3 == 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.
numbers = []
newNumbers = []
i = 0
while i < 100:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] % 5 == 0:
newNumbers.append(numbers[i])
if 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, # creates the menu, its components, and each component's price.
"fries": 1.99,
"drink": 0.99,
"checkout": 0} # 'checkout' is created in the table in order for it to be an interactive input.
total = 0 # creates the variable 'total' and sets it to 0.
print("Menu") # prints menu's components.
for k,v in menu.items(): # 'k' = item's name. 'v' = item's price(numeric value).
print(k + " $" + str(v)) #str changes a numeric value (v) into a character that can be printed.
print("Please select an item. Type 'checkout' to finish your order.") #tells users what they can do.
checkout = 0 # checkout created and set to 0.
while checkout == 0: # while loop connected to checkout.
item = input("Please select an item from the menu Type 'checkout' to finish.") #tells users what they can do.
item = item.lower() #changes input to lowercase to be user friendly.
if item == "burger" or item == "fries" or item == "drink": #this if statement allows for muiltple inputs to have the same output.
print( item, "will be $" + str(menu[item])) #prints item info.
total += menu[item] # adds the price of the item to total price.
else:
if item == "checkout":
checkout = 1 #Changes value of checkout that ends while loop.
else:
print("Try again, that item is not on the menu") #In case user makes a typo or tpyes an unknown item.
else:
myorder = "Your total is ${:.2f}! Enjoy!" #source: w3shools.com Python String Formatting
print(myorder.format(total))