Identifying and Correcting Errors Practice
Practice to correct code segments with errors
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabetList = []
for i in alphabet:
alphabetList.append(i)
print(alphabetList)
# case 1: using while loop
letter = input("What letter would you like to check?")
i = 0
pos = -1
while i < 26:
if alphabetList[i] == letter:
print("The letter " + letter + " is the " + str(i) + " letter in the alphabet")
pos = i
break
i += 1
# test case
if pos == -1:
print("The letter " + letter + " is not in the alphabet")
elif alphabetList[pos] != letter:
print("The letter " + letter + " is not the " + str(pos) + " letter in the alphabet")
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabetList = []
for i in alphabet:
alphabetList.append(i)
print(alphabetList)
# case 1: using for loop
# move the count = 0 outside of the for loop
letter = input("What letter would you like to check?")
count = 0
pos = -1
for i in alphabetList:
if i == letter:
print("The letter " + letter + " is the " + str(count) + " letter in the alphabet")
pos = count
break
count += 1
# test case
if pos == -1:
print("The letter " + letter + " is not in the alphabet")
elif alphabetList[pos] != letter:
print("The letter " + letter + " is not the " + str(pos) + " letter in the alphabet")
odds = []
i = 1
while i <= 10:
odds.append(i)
i += 2
print(odds)
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)
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])
elif numbers[i] % 2 == 0:
newNumbers.append(numbers[i])
print(newNumbers)
# Also test if user input is on the menu, e.g. if inputted 'pizza' it will output that it is not on the menu
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
while True:
item = input("Please select an item from the menu. Type q if done\n")
if item == 'q':
print("Thanks for your selection\n")
break
elif item not in menu.keys():
print(item + " is not in menu, please reselect\n")
else:
print("you have selected "+item+", price is "+str(menu[item])+"\n")
total += menu[item]
#code should add the price of the menu items selected by the user
print("Your total is "+str(total))