Identifying and Correcting Errors Notes
Showing understanding with execution. See code comments for comment.
This first function exists to tell the user where in the alphabet a given letter is. Below is some work to fix certain problems with the program, the corrections being detailed with Python comments.
alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabetList = []
for i in alphabet:
alphabetList.append(i)
print(alphabetList)
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") #1 has been added to i to account for zero-based syntax.
i += 1
letter = input("What letter would you like to check?")
count = 0 #Count has been moved out of the function so that it can be overwritten by the input.
for i in alphabetList:
if i == letter:
print("The letter " + letter + " is the " + str(count + 1) + " letter in the alphabet") #1 is added to count to account for zero-based syntax.
count += 1
The program below gives a list of even numbers less than or equal to 10. In the cell below it, we want it to print odd numbers instead.
evens = []
i = 0
while i <= 10:
evens.append(i)
i += 2
print(evens)
odds = []
i = 1 #The starting i value has been increased to one so that the list starts at 1 and 2 is added each loop.
while i <= 10:
odds.append(i)
i += 2
print(odds)
The program below extracts even numbers from a list of numbers from 0 to 10. We want it to print odd numbers instead.
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)
numbers = [0,1,2,3,4,5,6,7,8,9,10]
odds = []
for i in numbers:
if (numbers[i] % 2 == 1): #remainder has been set to 1, as odd numbers give a remainder of 1 when divided by 2
odds.append(numbers[i])
print(odds)
The program below lists numbers between 1 and 100 once if it is divisible by 2 or 5.
numbers = []
newNumbers = []
i = 0
while i <= 100: #we want to include 100, so it becomes "<=" instead of "<"
numbers.append(i)
i += 1
for i in numbers:
if numbers[i] == 0: #prevents 0 from being included, as it's between 1 and 100
pass
elif numbers[i] % 5 == 0:
newNumbers.append(numbers[i])
elif numbers[i] % 2 == 0: #"if" changed to "elif" so that both conditions are considered
newNumbers.append(numbers[i])
print(newNumbers)
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")
#code should add the price of the menu items selected by the user
print(total)
I input my order and the total remained at zero. It seems like there isn't any code in this program to add the price of the items in the dictionary menu
to the total price of the order. This is a matter of adding some code that is completely absent.
Please see the code comments to see all the things I changed as well as their purposes.
affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea', 'Yea', 'mhm', 'Mhm', 'yep', 'Yep', 'Affirmative,' 'affirmative', 'sure', 'Sure', 'alright', 'Alright', 'okay', 'Okay', 'OK', 'ok', 'Ok']
negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah', 'nuh-uh', 'Nuh-uh', 'negative', 'Negative']
#Capital and lowercase versions of all the responses are provided because this list was created before I knew how to force the string to be read as lower/uppercase
menu = {"Burger": 3.99,
"Fries": 1.99,
"Drink": 0.99}
#shows the user the menu and prompts them to select an item
print("Menu")
total = 0 #price is defined later here just so that it is defined first
ordered = False
def finish(): #function to allow the user to order another item
global fin
print("Is your order complete?")
msg = input()
fin = msg.lower() #corrects for possible capitalized user response
def finishcheck():
roundtotal = round(total, 2) #rounding to prevent certain sum errors with floats
print("Your current total is $" + str(roundtotal) + ".")
finish() #see just above
if fin in affirmative: #using affirmative bank to understand user responses
print("Understood.")
return 0 #value to be read later (0 = stop)
elif fin in negative: #same but with negative
print("Understood.")
return 1 #value to be read later (1 = continue)
else:
print("Invalid response.")
finish() #repeating process when an invalid response is provided
def order(): #defined as a function so that the process can be repeated
global total
global ordered
for k,v in menu.items(): #reprinting the menu for each order
print(k + " $" + str(v))
if ordered == True:
print("What else would you like to order?")
msg = input("Please select an item from the menu")
item = msg.lower()
if item == "burger": #I probably should've found a way to iterate this :/
total += 3.99
print("You ordered a burger.")
ordered = True
if finishcheck() == 0:
return
else: #using else to prevent a second instance of finishcheck()
order()
elif item == "fries":
total += 1.99
print("You ordered fries.")
ordered = True
if finishcheck() == 0:
return
else:
order()
elif item == "drink":
total += 0.99
print("You ordered a drink.")
ordered = True
if finishcheck() == 0:
return
else:
order()
else: #repeating the order process when an invalid input is provided
print("We do not serve that here.")
order()
order()
#code should add the price of the menu items selected by the user
roundtotal = round(total, 2) #rounding to prevent certain sum errors with floats
print("Your total is $" + str(roundtotal) + ". Thank you for choosing Python's Pub.")
I was dissatisfied with the check not being iterated, so I worked with my friend AJ to reverse-engineer and then understand the items()
function.
import string #used later to capitalize
#These two lists contain possible affirmative/negative responses (used later)
affirmative = ['yes', 'Yes', 'yeah', 'Yeah', 'yup', 'Yup', 'y', 'Y', 'yea', 'Yea', 'mhm', 'Mhm', 'yep', 'Yep', 'Affirmative,' 'affirmative', 'sure', 'Sure', 'alright', 'Alright', 'okay', 'Okay', 'OK', 'ok', 'Ok']
negative = ['No', 'no', 'Nope', 'nope', 'N', 'n', 'Nah', 'nah', 'nuh-uh', 'Nuh-uh', 'negative', 'Negative']
#Capital and lowercase versions of all the responses are provided because this list was created before I knew how to force the string to be read as lower/uppercase
menu = {"Burger": 3.99,
"Double Burger": 5.49,
"Fries": 1.99,
"Animal Fries": 2.79,
"Medium Drink": 0.99,
"Monster Drink": 1.49,
"Live Concert While You Eat": 499.99}
total = 0 #price is defined later here just so that it is defined first
ordered = False
receipt = []
def finish(): #function to allow the user to order another item
global fin
print("Is your order complete?")
msg = input()
fin = msg.lower() #corrects for possible capitalized user response
def finishcheck():
roundtotal = round(total, 2) #rounding to prevent certain sum errors with floats
print("Your current total is $" + str(roundtotal) + ".")
finish() #see just above
if fin in affirmative: #using affirmative bank to understand user responses
print("Understood.")
return 0 #value to be read later (0 = stop)
elif fin in negative: #same but with negative
print("Understood.")
return 1 #value to be read later (1 = continue)
else:
print("Invalid response.")
finish() #repeating process when an invalid response is provided
def ordercheck():
global total
global ordered
msg = input("Please select an item from the menu")
item = string.capwords(msg, sep = None)
for k,v in menu.items(): #for loop checks if input is one of the accepted dictionary keys
if item == k:
ordered = True
if k == "Fries":
print("You ordered fries.")
elif k == "Animal Fries":
print("You ordered animal fries.")
else:
print("You ordered a " + k.lower() + ".")
total += v
#receipt.append(v)
return 1
def order(): #defined as a function so that the process can be repeated
global ordered
if ordered == False:
print("-----MENU-----")
for k,v in menu.items(): #reprinting the menu for each order
print(k + " $" + str(v))
print("--------------")
else:
print("What else would you like to order?")
if ordercheck() == 1:
if finishcheck() == 0:
return
else:
order()
else:
print("We don't serve that here.")
order()
order()
roundtotal = round(total, 2) #rounding to prevent certain sum errors with floats
print("Your total is $" + str(roundtotal) + ". Thank you for choosing Python's Pub.")