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
- There are two changes you can make
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
letter = input("What letter would you like to check?")
count = 0
for i in alphabetList:
if i == letter:
print("The letter " + letter + " is the " + str(count+1) + " letter in the alphabet")
count += 1
evens = []
i = 0
while i <= 10:
evens.append(numbers[i])
i += 2
print(evens)
This code should output the odd numbers from 0 - 10 using a while loop.
evens = []
i = 0
while i <= 10:
evens.append(i)
i += 2
print(evens)
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
- 3 changes to the code will give the expected outcome.
numbers = []
newNumbers = []
i = 0
while i < 101:
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] % 10 == 5:
newNumbers.append(numbers[i])
if numbers[i] % 2 == 0:
newNumbers.append(numbers[i])
print(newNumbers)
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("Saavan Gade 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.Type 'done' when finished")
item = input("Please select items from the menu.Type 'done' when finished")
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))